search

Home  >  Q&A  >  body text

express - node.js中的promise回调的写法问题

请教正确的promise用法!!!

下面是错误的写法 最后没有return的结果就是 调用这个API函数拿不到promise

const apiFunction = () => {
    return Promise.resolve()
            .then(() => (demoResolve()))
            .then(() => (demoReject()))
    
            .then((data) => {
                // bug
                Promise.resolve(data);
            })
            .catch((err) => {
                // bug
                Promise.reject(err);
            });
};        

改进后下面这个promise方法能够如预期的拿到promise的值

const apiFunction = () => {
    return Promise.resolve()
            .then(() => (doResolve()))
            .then(() => (doReject()))
            // okay √
            .then((data) => Promise.resolve(data))
            // okay √
            .catch((err) => Promise.reject(err));
};
    

之后又尝试了一种写法,感觉这种写法和第一种写法没有什么区别,但是这样也写也是可以得到正确结果的。
请问这是什么情况呢?
这样写有错误吗?

const apiFunction = () => {
    return Promise.resolve()
            .then(() => (doResolve()))
            .then(() => (doReject()))
            .then(Promise.resolve)
            .catch(Promise.reject);
};

是不是等价于这种写法

const apiFunction = () => {
    return Promise.resolve()
            .then(() => (doResolve()))
            .then(() => (doReject()))
            .then(result)
            .catch(err);
};
PHPzPHPz2768 days ago629

reply all(3)I'll reply

  • 迷茫

    迷茫2017-04-17 15:27:08

    1. Compare ES6 => After ES5, the difference between the first and second way of writing is mainly whether the promise returns

    //----------------case 1-------------------------------
    var apiFunction = function apiFunction() {
        return Promise.resolve().then(function () {
            return demoResolve();
        }).then(function () {
            return demoReject();
        }).then(function (data) {
            // bug
            AL.Promise.resolve(data);
        }).catch(function (err) {
            // bug
            AL.Promise.reject(err);
        });
    };
    
    //---------------------------case2--------------------------------
    var apiFunction2 = function apiFunction2() {
        return Promise.resolve().then(function () {
            return doResolve();
        }).then(function () {
            return doReject();
        })
        // okay √
        .then(function (data) {
            return Promise.resolve(data);
        })
        // okay √
        .catch(function (err) {
            return Promise.reject(err);
        });
    };
    
    

    The difference between the third type and the first type is whether to return

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:27:08

    Where did the first wording AL. come from

    Update.

    Damn it, when I saw this question, the questioner was indeed written incorrectly, so I asked about it. Later, I came in again and saw that the other answers all said the correct answer, so I didn't care. Today, I was stepped on. I really feel aggrieved.


    Okay, let me tell you the answer again.

    The then of promise must be returned, which is used to generate a new promise for later use.

    When an anonymous arrow function returns an expression, it returns it by default.

    When written directly.then(Promise.resolve) it is equivalent to passing it into thenPromise.resolveThis method is used as a callback, which is of course correct.

    .then(result)This way of writing, I don’t know what the result is, it’s not mentioned in the question!

    In short, then must return at the end, which is used to generate new promises for subsequent chain calls.

    reply
    0
  • 阿神

    阿神2017-04-17 15:27:08

    Your first way of writing is not completely wrong, it’s just that you didn’t return the new then object in Promise.

    reply
    0
  • Cancelreply