Working with promise and generators
2014-08-23 07:32
375 查看
An example for working with Q and generators.
You can run it in nodejs 0.11 harmony model
You can run it in nodejs 0.11 harmony model
var Q = require('q'); function asyncSquare(n) { var defer = Q.defer(); setTimeout(function(){ if(n>5) defer.reject(888) defer.resolve(n*n); },2000) return defer.promise; } // ES6 using generator that yields promises to Q.spawn Q.spawn(function *() { try{ var a = yield asyncSquare(5); console.log(a); var b = yield asyncSquare(6); console.log(b); console.log('all done'); } catch(err){ console.log("err:"+err) } });
相关文章推荐
- Working with Validators and Messages in AngularJS
- Android: Simplified source code for parsing and working with XML data and web services in Android
- Drupal Working with nodes, content types and fields
- Working with JSONPath and JavaScript
- Android: Reading, using and working with XML data and web services in Android
- db2入门实验(Working with Instances and Databases )
- Working with BeforeProperties and AfterProperties on SPItemEventReceiver
- Quartz Scheduler(2.2.1) - Working with TriggerListeners and JobListeners
- Working with Preferences and Saving State
- Promise and Add-on, Think Asynchronously with Callback!
- GP学习(九)—Working with geodatabases and the geoprocessor
- 《Windows via C/C++》学习笔记(二):Working with Characters and String
- CLR Via C# 3rd 阅读摘要 -- Chapter 14 - Chars, Strings, and Working with Text
- MDX Step by Step 读书笔记(四) - Working with Sets (使用集合) - Limiting Set and Auto-Exists
- Working with DbGeography, Points and Polygons in .NET
- Section 5: Working with Files and Directories
- 精彩控件源码(2)-Farsi Library - Working with Dates, Calendars, and DatePickers
- Working with hashCode and equals methods in java
- Promise with fetch and post
- [转]Working with Parameters and Return Codes in the Execute SQL Task