Changeset 489 for Dev/trunk/src/node_modules/q/README.md
- Timestamp:
- 03/08/14 11:41:10 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/node_modules/q/README.md
r484 r489 35 35 36 36 ```javascript 37 Q.fcall( step1)38 .then( step2)39 .then( step3)40 .then( step4)37 Q.fcall(promisedStep1) 38 .then(promisedStep2) 39 .then(promisedStep3) 40 .then(promisedStep4) 41 41 .then(function (value4) { 42 42 // Do something with value4 43 }, function (error) { 44 // Handle any error from step1 through step4 43 }) 44 .catch(function (error) { 45 // Handle any error from all above steps 45 46 }) 46 47 .done(); 47 48 ``` 48 49 49 With this approach, you also get implicit error propagation, 50 just like ``try``, ``catch``, and ``finally``. An error in 51 ``step1`` will flow all the way to ``step5``, where itâs52 caught and handled. 50 With this approach, you also get implicit error propagation, just like `try`, 51 `catch`, and `finally`. An error in `promisedStep1` will flow all the way to 52 the `catch` function, where itâs caught and handled. (Here `promisedStepN` is 53 a version of `stepN` that returns a promise.) 53 54 54 55 The callback approach is called an âinversion of controlâ. … … 77 78 78 79 Q can exchange promises with jQuery, Dojo, When.js, WinJS, and more. 79 Additionally, there are many libraries that produce and consume Q promises for 80 everything from file system/database access or RPC to templating. For a list of 81 some of the more popular ones, see [Libraries][]. 82 83 Please join the Q-Continuum [mailing list](https://groups.google.com/forum/#!forum/q-continuum). 84 80 81 ## Resources 82 83 Our [wiki][] contains a number of useful resources, including: 84 85 - A method-by-method [Q API reference][reference]. 86 - A growing [examples gallery][examples], showing how Q can be used to make 87 everything better. From XHR to database access to accessing the Flickr API, 88 Q is there for you. 89 - There are many libraries that produce and consume Q promises for everything 90 from file system/database access or RPC to templating. For a list of some of 91 the more popular ones, see [Libraries][]. 92 - If you want materials that introduce the promise concept generally, and the 93 below tutorial isn't doing it for you, check out our collection of 94 [presentations, blog posts, and podcasts][resources]. 95 - A guide for those [coming from jQuery's `$.Deferred`][jquery]. 96 97 We'd also love to have you join the Q-Continuum [mailing list][]. 98 99 [wiki]: https://github.com/kriskowal/q/wiki 100 [reference]: https://github.com/kriskowal/q/wiki/API-Reference 101 [examples]: https://github.com/kriskowal/q/wiki/Examples-Gallery 85 102 [Libraries]: https://github.com/kriskowal/q/wiki/Libraries 103 [resources]: https://github.com/kriskowal/q/wiki/General-Promise-Resources 104 [jquery]: https://github.com/kriskowal/q/wiki/Coming-from-jQuery 105 [mailing list]: https://groups.google.com/forum/#!forum/q-continuum 86 106 87 107 … … 109 129 guarantee when mentally tracing the flow of your code, namely that 110 130 ``then`` will always return before either handler is executed. 131 132 In this tutorial, we begin with how to consume and work with promises. We'll 133 talk about how to create them, and thus create functions like 134 `promiseMeSomething` that return promises, [below](#the-beginning). 111 135 112 136 … … 321 345 var funcs = [foo, bar, baz, qux]; 322 346 323 var result = Q .resolve(initialVal);347 var result = Q(initialVal); 324 348 funcs.forEach(function (f) { 325 349 result = result.then(f); … … 333 357 return funcs.reduce(function (soFar, f) { 334 358 return soFar.then(f); 335 }, Q .resolve(initialVal));359 }, Q(initialVal)); 336 360 ``` 337 361 … … 518 542 var deferred = Q.defer(); 519 543 Q.when(promise, deferred.resolve); 520 Q.when(delay(ms),function () {544 delay(ms).then(function () { 521 545 deferred.reject(new Error("Timed out")); 522 546 }); … … 617 641 618 642 ```javascript 619 return Q .when($.ajax(...))643 return Q($.ajax(...)) 620 644 .then(function () { 621 645 }); … … 685 709 686 710 If you're working with functions that make use of the Node.js callback pattern, 687 Q provides a few useful utility functions for converting between them. The 688 most straightforward are probably `Q.nfcall` and `Q.nfapply` ("Node function 689 call/apply") for calling Node.js-style functions and getting back a promise: 711 where callbacks are in the form of `function(err, result)`, Q provides a few 712 useful utility functions for converting between them. The most straightforward 713 are probably `Q.nfcall` and `Q.nfapply` ("Node function call/apply") for calling 714 Node.js-style functions and getting back a promise: 690 715 691 716 ```javascript … … 776 801 to many users, you should probably keep it off. But in development, go for it! 777 802 778 ## Reference779 780 A method-by-method [Q API reference][reference] is available on the wiki.781 782 [reference]: https://github.com/kriskowal/q/wiki/API-Reference783 784 ## More Examples785 786 A growing [examples gallery][examples] is available on the wiki, showing how Q787 can be used to make everything better. From XHR to database access to accessing788 the Flickr API, Q is there for you.789 790 [examples]: https://github.com/kriskowal/q/wiki/Examples-Gallery791 792 803 ## Tests 793 804 … … 796 807 [tests]: https://rawgithub.com/kriskowal/q/master/spec/q-spec.html 797 808 798 --- 799 800 Copyright 2009 -2012Kristopher Michael Kowal809 ## License 810 811 Copyright 2009â2013 Kristopher Michael Kowal 801 812 MIT License (enclosed) 802 813
Note: See TracChangeset
for help on using the changeset viewer.