Conjuring JQuery Deferred with monadic incantations

前端 未结 3 949
名媛妹妹
名媛妹妹 2021-01-13 06:34

Inspired by this (excellent) discussion of using Promises in javascript, I\'m trying to work out how I could use Deferred to chain together async and non-async functions, to

3条回答
  •  臣服心动
    2021-01-13 06:58

    Wrapping a value into a promise is as simple as using $.when:

    var promise = $.when( value );
    

    Also, as of jQuery 1.6, you have a very simple chaining method (pipe):

    var chained = functionThatReturnsAPromise().pipe(function( resolveValue ) {
            return functionThatReturnsAnotherPromise( resolveValue );
        });
    
    chained.done(function() {
        // Both asynchronous operations done in sequence
    });
    

    Hope this helps.

提交回复
热议问题