Function Returns a Promise, Check Errors

后端 未结 2 816
别跟我提以往
别跟我提以往 2021-01-25 18:44

I have a function doSomething() that returns a promise chain, utilizing the Q framework. The contents are similar to something like:

loadDataSet :          


        
2条回答
  •  情深已故
    2021-01-25 19:06

    You can't throw exceptions inside then as no one will able to catch it. Instead, create a new Q.deferand call reject on it whenever there's an error

    loadDataSet : function (params) {
        var deferred = Q.defer()
        Q.fcall(function() {
            //Do Something
        }).then(function(){
           //Do Something Else
           deferred.reject('error message')
        }, deferred.reject)
        return deferred.promise
    }
    

    then use it like this

    loadDataSet().then(function (data) {
        //ok, got data
    }).catch(function (err) {
        //error!
    })
    

提交回复
热议问题