JS Promise - instantly retrieve some data from a function that returns a Promise

前端 未结 6 1171
生来不讨喜
生来不讨喜 2021-01-04 20:07

Can anyone recommend a pattern for instantly retrieving data from a function that returns a Promise?

My (simplified) example is an AJAX preloader:

lo         


        
6条回答
  •  一整个雨季
    2021-01-04 20:54

    The method I am currently using is as follows:

    var optionalReturnsObject = {};
    functionThatReturnsPromise(dataToSend, optionalReturnsObject ).then(doStuffOnAsyncComplete);
    console.log("Some instant data has been returned here:", optionalReturnsObject ); 
    

    For me, the advantage of this is that another member of my team can use this in a simple way:

    functionThatReturnsPromise(data).then(...);
    

    And not need to worry about the returns object. An advanced user can see from the definitions what is going on.

提交回复
热议问题