progress notifications in ECMAScript Promise

前端 未结 2 696
一向
一向 2020-12-08 10:07

We\'re using ECMAScript 6 promises.

We need to implement progress notifications to the end-user (this is pure UX requirement). I know that other promise

2条回答
  •  离开以前
    2020-12-08 10:33

    Since only specific instance of promise generates progress, we can monkey patch it on demand, like this:

       function reparse(){
            let notify
            let promise = new Promise(async(resolve,reject)=>{
                instanceOfjQueryDeferred.done(()=>{
                    resolve(100)
                }).progress((progress)=>{
                    notify(progress)
                })
            })
            // here is the monkey patch
            promise.progress  = (handler)=>{
                notify = handler
                return promise
            }
            return promise
        }
    

    And use it like this:

    reparse().progress((p)=>{
        console.log('progress',p)
    }).then((progress)=>{
        console.log('done',progress)
    })
    

提交回复
热议问题