node-promise在Node.js平台上提供了完整的Promises的实现。
Thenode-promisemodulefeaturesapromiseimplementationwith:
ChainablepromisesPromisesthrowerrorsifanerrorhandlerisnotprovidedCommonJSpromiseproposal[1]compliantImmutableoncefulfilledtoreducepossibleside-effectsPromisescanbeusedsecurely(asseparateresolver/promisepairsinocapsituations)Backwardscompatibilitywherepossible(addCallback,addErrback,emitSuccess,andemitErrorshouldstillbehaveasexpected)Utilityfunctions,including:
when()-Normalizationofsync(normalvalues)andasync(promises)all()-Createapromisethataccumulatemultipleconcurrentpromisesfirst()-Findthefirstpromisetobefulfilledinagroupofpromisesseq()-Sequentiallyexecuteasetofpromisereturningfunctionsdelay()-Returnsapromisethatisfulfilledafteragivenamountoftimeexecute()-Executesafunctionthattakesacallbackandreturnsapromise(thankyouBenjaminThomasforprovidingthis)示例代码:
varPromise=require("promise").Promise;varpromise=newPromise();asyncOperation(function(){Promise.resolve("succesfulresult");});promise->giventotheconsumer
评论