From 890861be03a5bbfddc4fea48b952113002b4fb02 Mon Sep 17 00:00:00 2001 From: Ruan Yifeng Date: Sun, 1 Mar 2015 13:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9promise/async?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/promise.md | 10 ++++++++-- docs/reference.md | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/promise.md b/docs/promise.md index a0779c4..0e3ca40 100644 --- a/docs/promise.md +++ b/docs/promise.md @@ -439,10 +439,16 @@ async函数并不属于ES6,而是被列入了ES7,但是traceur编译器和re ```javascript async function getStockPrice(symbol, currency) { - let price = await getStockPrice(symbol); - return convert(price, currency); + let price = await getStockPrice(symbol); + return convert(price, currency); } +getStockPrice("JNJ") + .then( + price => console.log(price), + error => console.error(error) + ); + ``` 上面代码是一个获取股票报价的函数,函数前面的async关键字,表明该函数将返回一个Promise对象。调用该函数时,当遇到await关键字,立即返回它后面的表达式(getStockPrice函数)产生的Promise对象,不再执行函数体内后面的语句。等到getStockPrice完成,再自动回到函数体内,执行剩下的语句。 diff --git a/docs/reference.md b/docs/reference.md index b0c364c..78de7af 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -67,6 +67,7 @@ - jmar777, [What's the Big Deal with Generators?](http://devsmash.com/blog/whats-the-big-deal-with-generators) - Marc Harter, [Generators in Node.js: Common Misconceptions and Three Good Use Cases](http://strongloop.com/strongblog/how-to-generators-node-js-yield-use-cases/): 讨论Generator函数的作用 - Axel Rauschmayer, [Iterators and generators in ECMAScript 6](http://www.2ality.com/2013/06/iterators-generators.html): 探讨Iterator和Generator的设计目的 +- Axel Rauschmayer, [Iterables and iterators in ECMAScript 6](http://www.2ality.com/2015/02/es6-iteration.html): Iterator的详细介绍 - StackOverflow, [ES6 yield : what happens to the arguments of the first call next()?](http://stackoverflow.com/questions/20977379/es6-yield-what-happens-to-the-arguments-of-the-first-call-next): 第一次使用next方法时不能带有参数 - Kyle Simpson, [ES6 Generators: Complete Series](http://davidwalsh.name/es6-generators): 由浅入深探讨Generator的系列文章,共四篇 - Gajus Kuizinas, [The Definitive Guide to the JavaScript Generators](http://gajus.com/blog/2/the-definetive-guide-to-the-javascript-generators): 对Generator的综合介绍 @@ -84,6 +85,7 @@ - Axel Rauschmayer, [ECMAScript 6 promises (2/2): the API](http://www.2ality.com/2014/10/es6-promises-api.html): 对ES6 Promise规格和用法的详细介绍 - Jack Franklin, [Embracing Promises in JavaScript](http://javascriptplayground.com/blog/2015/02/promises/): catch方法的例子 - Luke Hoban, [Async Functions for ECMAScript](https://github.com/lukehoban/ecmascript-asyncawait): Async函数的设计思想,与Promise、Gernerator函数的关系 +- Jafar Husain, [Asynchronous Generators for ES7](https://github.com/jhusain/asyncgenerator): Async函数的深入讨论 ## Class与模块