promise-then 深切明白异步

异步经常运用的几种体式格局

setTimeout/EventEmitter/Promise/generator/async-await 本日偏重来解说一下 promise

setTimeout和Promise行列有何关联

setTimeout(function(){console.log(4)},0); 
new Promise(function(resolve){ 
    console.log(1) 
    for( var i=0 ; i<10000 ; i++ ){ i==9999 && resolve() } 
    console.log(2) })
.then(function(){ console.log(5) });
 console.log(3);

在这里我们须要先明白We have a problem with promises

    doSomething().then(function () {
      return doSomethingElse();
    });
    
    doSomething().then(function () {
      doSomethingElse();
    });
    
    doSomething().then(doSomethingElse());
    
    doSomething().then(doSomethingElse);

promise 在运用的过程当中须要注重的处所

1.new promise (resolve,reject){};肯定要resolve,或许reject 出去不要,做副作用函数。
2.then 操纵内里 吸收 promise 对象,假如在promise内里是实行的纯函数,也可以返回一个 promise。resolve("常量")。防止promise 穿透

准确运用 promise 金字塔代码

新手毛病 #1: promise版的金字塔题目
视察人人怎样运用 PouchDB 这类大型的 promise 作风的API,我发明大批毛病的 promise 运用情势。最常见的毛病就是下面这个:

remotedb.allDocs({
  include_docs: true,
  attachments: true
}).then(function (result) {
  var docs = result.rows;
  docs.forEach(function(element) {
    localdb.put(element.doc).then(function(response) {
      alert("Pulled doc with id " + element.doc._id + " and added to local db.");
    }).catch(function (err) {
      if (err.status == 409) {
        localdb.get(element.doc._id).then(function (resp) {
          localdb.remove(resp._id, resp._rev).then(function (resp) {

// et cetera…
是的,实际上你可以像运用回调一样运用 promises,恩,就像用打磨机去削脚趾甲一样,你确切可以这么做。

而且假如你认为如许的毛病只限于初学者,那末你会惊奇于我实际上是在黑莓官方开辟者博客上看到上面的代码。老的回调作风的习气难以祛除。(至开辟者: 抱歉选了你的例子,然则你的例子将会有主动的教诲意义)

准确的作风应该是如许:

remotedb.allDocs(...).then(function (resultOfAllDocs) {
  return localdb.put(...);
}).then(function (resultOfPut) {
  return localdb.get(...);
}).then(function (resultOfGet) {
  return localdb.put(...);
}).catch(function (err) {
  console.log(err);
});

这类写法被称为 composing promises ,是 promises 的壮大才能之一。每个函数只会在前一个 promise 被挪用而且完成回调后挪用,而且这个函数会被前一个 promise 的输出挪用,稍后我们在这块做更多的议论。

  • 4.promise 中foreach 操纵,转换成 promise.all()=>返回一个数组
  • 5.在任何运用到了promise的过程当中,肯定要注重运用catch 将异常抛出来,不然会梗塞全部加载

运用副作用挪用而非返回

下面的代码有什么题目?

somePromise().then(function () {
  someOtherPromise();
}).then(function () {
  // Gee, I hope someOtherPromise() has resolved!
  // Spoiler alert: it hasn't.
});

好了,现在是时刻议论一下关于 promises 你所须要知道的统统。

仔细的说,这是一个一旦你明白了它,就会防止一切我说起的毛病的离奇的技能。你预备好了么?

就如我前面所说,promises 的巧妙在于赋予我们之前的 return 与 throw。然则在实践中这究竟是怎样一回事呢?

每个 promise 都邑供应给你一个 then() 函数 (或是 catch(),实际上只是 then(null, …) 的语法糖)。当我们在 then() 函数内部时:

somePromise().then(function () {
  // I'm inside a then() function!
});

我们可以做什么呢?有三种事变:

return 另一个 promise
return 一个同步的值 (或许 undefined)
throw 一个同步异常
就是如许。一旦你明白了这个技能,你就明白了 promises。因而让我们逐一了解下。

返回另一个 promise
这是一个在 promise 文档中常见的运用形式,也就是我们在上文中提到的 “composing promises”:

getUserByName('nolan').then(function (user) {
  return getUserAccountById(user.id);
}).then(function (userAccount) {
  // I got a user account!
});

注重到我是 return 第二个 promise,这个 return 异常重要。假如我没有写 returngetUserAccountById() 就会成为一个副作用,而且下一个函数将会吸收到 undefined 而非 userAccount
返回一个同步值 (或许 undefined)
返回 undefined 通常是毛病的,然则返回一个同步值实际上是将同步代码包裹为 promise 作风代码的一种异常赞的手腕。举例来说,我们对 users 信息有一个内存缓存。我们可以如许做:

getUserByName('nolan').then(function (user) {
  if (inMemoryCache[user.id]) {
    return inMemoryCache[user.id];    // returning a synchronous value!
  }
  return getUserAccountById(user.id); // returning a promise!
}).then(function (userAccount) {
  // I got a user account!
});

是否是很赞?第二个函数不须要体贴 userAccount 是从同步要领照样异步要领中猎取的,而且第一个函数可以异常自在的返回一个同步或许异步值。

不幸的是,有一个不方便的现实是在 JavaScript 中无返回值函数在技术上是返回 undefined,这就意味着当你本意是返回某些值时,你很随意马虎会不经意间引入副作用。

出于这个缘由,我个人养成了在 then() 函数内部 永久返回或抛出 的习气。我发起你也如许做。

抛出同步异常
谈到 throw,这是让 promises 越发赞的一点。比方我们愿望在用户已登出时,抛出一个同步异常。这会异常简朴:

getUserByName('nolan').then(function (user) {
  if (user.isLoggedOut()) {
    throw new Error('user logged out!'); // throwing a synchronous error!
  }
  if (inMemoryCache[user.id]) {
    return inMemoryCache[user.id];       // returning a synchronous value!
  }
  return getUserAccountById(user.id);    // returning a promise!
}).then(function (userAccount) {
  // I got a user account!
}).catch(function (err) {
  // Boo, I got an error!
});

假如用户已登出,我们的 catch() 会吸收到一个同步异常,而且假如 后续的 promise 中涌现异步异常,他也会吸收到。再强调一次,这个函数并不须要体贴这个异常是同步照样异步返回的。

这类特征异常有效,因而它可以在开辟过程当中协助定位代码题目。举例来说,假如在 then() 函数内部中的任何处所,我们实行 JSON.parse(),假如 JSON 花样是毛病的,那末它就会抛出一个异常。假如是运用回调作风,这个毛病极可能就会被吃掉,然则运用 promises,我们可以随意马虎的在 catch() 函数中处置惩罚它了。

肯定要注重我们在then 函数内部的时刻

我们可以做什么呢?有三种事变:

return 另一个 promise
return 一个同步的值 (或许 undefined)
throw 一个同步异常

参考资料

    原文作者:木子喵
    原文地址: https://segmentfault.com/a/1190000013778006
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞