最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Promises and setTimeout - Stack Overflow

programmeradmin3浏览0评论

I have been playing with Promises, but I am having trouble understanding what is happening with the following code:

function a() { 
    return new Promise(function (resolve, reject) { 
        resolve("hi from a!");
    });
}

function b() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from b!");
        }, 5000);
    });
}

function c() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from c!");
        }, 1000);
    });
}

a().then(function (resultFromA) {
    console.log("a() responded with: " + resultFromA);
    b();
}).then(function (resultFromB) { 
    console.log("b() responded with: " + resultFromB);
    c();
}).then(function (resultFromC) { 
    console.log("c() responded with: " + resultFromC);
});

I am expecting this to output a() responded with: hi from a! immediately, along with b() responded with: hi from b! and c() responded with: hi from c! after their respective setTimeout() fires. However, what I get this the following output immediately:

a() responded with: hi from a!

b() responded with: undefined

c() responded with: undefined

I was thinking that .then() waits on these promises, but it isn't. Any help would be appreciated.

I have been playing with Promises, but I am having trouble understanding what is happening with the following code:

function a() { 
    return new Promise(function (resolve, reject) { 
        resolve("hi from a!");
    });
}

function b() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from b!");
        }, 5000);
    });
}

function c() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from c!");
        }, 1000);
    });
}

a().then(function (resultFromA) {
    console.log("a() responded with: " + resultFromA);
    b();
}).then(function (resultFromB) { 
    console.log("b() responded with: " + resultFromB);
    c();
}).then(function (resultFromC) { 
    console.log("c() responded with: " + resultFromC);
});

I am expecting this to output a() responded with: hi from a! immediately, along with b() responded with: hi from b! and c() responded with: hi from c! after their respective setTimeout() fires. However, what I get this the following output immediately:

a() responded with: hi from a!

b() responded with: undefined

c() responded with: undefined

I was thinking that .then() waits on these promises, but it isn't. Any help would be appreciated.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 8, 2016 at 3:02 Brian KiefferBrian Kieffer 3145 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

You need to return b() and return c() from within your then handlers.

then only "replaces" the first promise with a subsequent promise which is returned from its callback.

If your then callback doesn't return a promise, then the then applies to the original promise, and it will be executed immediately regardless of the contents/result of the previous then callback.

Basically...

a().then(function () {
  b()
}).then( # This "then" is chained off of a's promise

While conversely:

a().then(function () {
  return b()
}).then( # This "then" is chained off of b's promise

You need to return promises to chain them :

a().then(function (resultFromA) {
  console.log("a() responded with: " + resultFromA);
  // return b() promise
  return b();
}).then(function (resultFromB) { 
  console.log("b() responded with: " + resultFromB);
  // return c() promise
  return c();
}).then(function (resultFromC) { 
  console.log("c() responded with: " + resultFromC);
});

You forgot to return from function call. Javascript function does not return implicitly.

function a() { 
    return new Promise(function (resolve, reject) { 
        resolve("hi from a!");
    });
}

function b() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from b!");
        }, 5000);
    });
}

function c() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from c!");
        }, 1000);
    });
}

a().then(function (resultFromA) {
    console.log("a() responded with: " + resultFromA);
    return b(); // return 
}).then(function (resultFromB) { 
    console.log("b() responded with: " + resultFromB);
    return c(); // return
}).then(function (resultFromC) { 
    console.log("c() responded with: " + resultFromC);
});
发布评论

评论列表(0)

  1. 暂无评论