使用ES6 Promises惯用处理前置条件 [英] Idiomatically handling pre-conditions with ES6 Promises

查看:152
本文介绍了使用ES6 Promises惯用处理前置条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ES6 Promises的新手并且一直在做研究。我在NodeJS中执行了一些执行异步工作的代码,但是我有一些必须首先检查的前置条件。我正在寻找惯用的最佳实践来处理这个(如果存在这样的事情)以及一些推理。我希望获得一个理解,因为我已经有了工作代码。

I am new to ES6 Promises and have been doing research. I have some code executing in NodeJS that does some async work, but I have a few pre-conditions that must be checked first. I'm seeking the idiomatic best-practice for dealing with this (if such a thing exists) along with some reasoning. I hope to gain an understanding, as I already have working code.

考虑以下虚构代码:

function doStuff(data, cb) {
     if (!data) {
         return cb(new Error("Don't be an idiot"));
     }

     externalLibrary.doSomethingCallbackAsync(data, cb);
}

如果我将此转换为promise-land,我会看到两种选择。

Were I to translate this into promise-land, I see two options.

选项1 ,我可以在承诺中包含先决条件。

Option 1, I can include the pre-condition in the promise.

function doStuff(data){
    return new Promise((resolve, reject) => {
        if (!data) {
            return reject(new Error("Don't be an idiot"));
        }

        externalLibrary.doSomethingCallbackAsync(data, function(err, newData) {
            if (err) {
                return reject(err);
            }
            return resolve(newData);
        });
    });
}






选项2 ,我可以在承诺之前运行前置条件。我不确定我是否理解 Promise.reject()的意图,但这似乎符合这个法案,允许我返回一个立即被拒绝的承诺。


Option 2, I can run the pre-condition prior to the promise. I'm not certain I understand the intent of Promise.reject(), but it seems to fit the bill here by allowing me to return an immediately rejected promise.

function doStuff(data){
    if (!data) {
        return Promise.reject(new Error("Don't be an idiot"));
    }

    return new Promise((resolve, reject) => {
        externalLibrary.doSomethingCallbackAsync(data, function(err, newData) {
            if (err) {
                return reject(err);
            }
            return resolve(newData);
        });
    });
}






我更喜欢选项2 出于可读性原因,但我不完全理解 Promise.reject()而我担心选项2 是滥用它。重申一下,我正在寻找最佳实践解决方案。


I prefer Option 2 for readability reasons, but I don't fully understand Promise.reject() and I am concerned that Option 2 is misusing it. Just to reiterate, I am looking for a best-practice solution.

推荐答案


我不是我确定理解 Promise.reject()的意图,但它似乎适合这里的账单

I'm not certain I understand the intent of Promise.reject(), but it seems to fit the bill here

你已经理解正确了。它明确地用于此类用例。去吧,它最好的做法。

You've understood it correctly. It's explicitly made for use cases such as this. Go for it, it is the best practice.

选项2的优点是它可以更好地使用已经返回承诺的API (想想返回externalLibrary.doSomethingAsync(data)),并且 承诺构造函数反模式

The advantage of Option 2 is that it works much cleaner with APIs that already do return promises (think return externalLibrary.doSomethingAsync(data)), and has less chance to fall for the Promise constructor antipattern.

这篇关于使用ES6 Promises惯用处理前置条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆