小编csw*_*swl的帖子

宣传同步功能

所以,我有一个像这样的代码.

     getSomethingAsync(something)
     .then(doSomethingAsync)
     .then(function(d) {
     _d = doSomethingSync(d);
     return doSomethingAsyncNext(_d);
     })
     .then(function(val) {
      //All done
      })
      .catch(err_handler);
Run Code Online (Sandbox Code Playgroud)

我想把它变成类似的东西.

     getSomethingAsync(something)
     .then(doSomethingAsync)
     .then(doSomethingSync)
     .then(doSomethingAsyncNext)
     .then(function(val) {
      //All done
      })
      .catch(err_handler);
Run Code Online (Sandbox Code Playgroud)

我应该改变doSomethingSync,它是:

      function(data) {
      // do a lot of things with data, throw errors for invalid data
      return changed_data;
      }
Run Code Online (Sandbox Code Playgroud)

至:

      function(data) {
      // do a lot of things with data, throw errors for invalid data
      return new Promise(function(resolve,reject){
      resolve(changed_data);
      });
      }
Run Code Online (Sandbox Code Playgroud)

要么:

      function(data) {
      return new Promise(function(resolve,reject){
      // do a lot of …
Run Code Online (Sandbox Code Playgroud)

javascript promise es6-promise

0
推荐指数
1
解决办法
96
查看次数

标签 统计

es6-promise ×1

javascript ×1

promise ×1