Keeping Promise Chains Readable

拜拜、爱过 提交于 2019-12-02 06:54:03

Well, you can use something like that with promises:

myArray.map(x => convertX)
  .filter()
  .whatever()
  .etc()

if you use my rsp module from npm.

Other than that you can use the async/await features of ES2017 to simplify the promises chains, especially their scope.

Because with code like this:

db.query(first query)
  .then(storeFirstQuery)
  .then(secondQueryAndStoreIt)
  .then(thirdQueryAndStoreIt)
  .catch(errHandlingFunction)

if you need to use the result of first query in the last thirdQueryAndStoreIt() handler, you have a problem with accessing the data that is out of scope. But when you do:

try {
    let a = await db.query(first query);
    let b = await storeFirstQuery();
    let c = await secondQueryAndStoreIt();
    let d = await thirdQueryAndStoreIt(a); // use 'a' here
} catch (e) {
    errHandlingFunction(e);
}

then you don't have the problem with scope, as you can easily access all of the previously assigned variables.

See this for Node versions that support this syntax:

You can use it with Node v7.6+ out of the box or with Node v7.0+ with the --harmony flag.

For older Node versions you can use co or Bluebird.coroutine for a similar syntax using generator functions and yield instead of await.

if you really want, you can limit scope to a single meta-Promise by creating it yourself:

return new Promise((resolve, reject) => {
  const f1 = () => { /* ... */ };
  const f2 = () => { /* ... */ };
  const f3 = () => { /* ... */ };

  return db.query()
    .then(f1)
    .then(f2)
    .then(f3)
    .then(resolve)
    .catch(reject);
});

but the most legible way to do this is to use async/await.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!