资讯专栏INFORMATION COLUMN

[gist]Why Promises will make async easy

EasonTyler / 2567人阅读

摘要:有许多理由你将会需要用到好吧先来解释一下什么是。这类似于我们经常使用的返回的那个玩意比如想要在对请求来的数据做加工

from http://oyanglul.us

What is Promises

Promise is a proxy for a value not knowing when its creation time. It provide 2 Methods then and catch, which return promise so they can be chained.

Promise.prototype.then(onFulfilled, onRejected)

Appends fullfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.

Promise.prototype.catch(onRejected)

Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.

Why Promises

there are so many reasons you should use promise in you client rich project if you are not using any MV* frameworks.

有许多理由你将会需要用到 Promises. 好吧先来解释一下什么是 Promises 。 这类似于我们经常使用的 jQuery.ajax() 返回的那个玩意 – Deferreds.

比如想要在对 ajax 请求来的数据做加工

  $.ajax("yourdata.url").then(function(data){
      // do something with your data
  })

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85259.html

相关文章

  • promise, async, await, execution order

    摘要: async can be transformed to promise. So, if we want to understand async, we have to understand promise first. Promise Normally, promise is easy to understand, especially when using like this: p...

    neuSnail 评论0 收藏0
  • JavaScript Promise API

    摘要:我们使用关键字和提供的和回调函数来创建新的根据异步任务的返回结果,开发者可以在回调函数体的内部手动调用或者。第一个方法的回调函数接收方法里的值当被时,会调用回调方法。如果被,而方法在之后,那回调函数永远不会被执行。 尽管同步代码易于追踪和调试,但异步代码普遍在性能和灵活性上更具优势。Why hold up the show when you can trigger numerous r...

    peixn 评论0 收藏0
  • Koa / Co / Bluebird or Q / Generators / Promises /

    摘要:经常游荡在的我总能发现许多好问题和好答案。尽管网络上有着各式各样的关于该主题的指导,但涉及到在各种情景下的最佳实践,或者较好实践的方面还是不够清晰。我寄希望于针对我这篇裹脚布式问题的回复可以改变这一现状。我感觉因此收益的绝对不止是我一个人。 经常游荡在 SO 的我总能发现许多好问题和好答案。它们的好不仅仅在于知识的价值,更可贵之处在于如何表达:如何提问/如何回答。不久前我在 SF...

    xingpingz 评论0 收藏0
  • [gist]pure and secure javascript oauth with yql

    from http://oyanglul.us It would be awesome if we can use OAuth in JavaScript purely in client side. before start to do that, please let me e...

    ityouknow 评论0 收藏0
  • js错误处理权威指北

    摘要:第一个是,是你传递给异常的构造函数的参数,比如你可以使用属性来访问到该消息第二个参数是异常堆栈跟踪,非常重要。异常产生后能在后端正确处理是的关键部分。我将向你展示自定义构造函数和错误代码的方法,我们可以轻松地将其传递给前端或任何调用者。 By Lukas Gisder-Dubé | nov 14, 2018 原文 接着我上一篇文章,我想谈谈异常。我肯定你之前也听过——异常是个好东西。一...

    lykops 评论0 收藏0

发表评论

0条评论

EasonTyler

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<