简体   繁体   English

区别:回报承诺和回报承诺。

[英]difference: return promise and return promise.then

I have such method in my factory 我工厂有这种方法

getTogether() {
        let promise = this._$q.all([this._$http.get('/groups'), this._$http.get('/tasks')]);
        return promise
            .then((data) => {
                //local variable for reduce server<->client
                this._localList = data;
                return this._localList;
            });
    };

and I call this method in my controller. 然后在控制器中调用此方法

this._boardFactory.getTogether().then((list) => {
            console.log(list)
        });

all works perfectly, however I don't understand one thing. 一切正常,但是我不明白一件事。

what's the difference between return promise and return promise.then ? 什么是收益之间的差异promise和回报promise.then
thanks for your attantion 谢谢你的参加

If you only do return promise in your code, you will return the resolution of the promise object. 如果仅在代码中return promise ,则将返回promise对象的分辨率。 However, if you do return promise.then you will return the resolution of the promise from the then clause. 但是,如果您确实return promise.then ,那么您将从then子句中return promise.then的解决方案。

then returns a promise, so promise.then will execute promise then execute the then clause returning that promise. then返回一个promise,这样promise.then将执行promise然后执行then子句返回该promise。

More info on then here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then 关于更多信息then点击这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

I say Promise is object and then() is method on Promise object which reruns another Promise. 我说Promise是对象, then()是Promise对象上的方法,该方法会重新运行另一个Promise。

Promise.prototype.then(onFulfilled, onRejected) - Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler, or to its original settled value if the promise was not handled (ie if the relevant handler onFulfilled or onRejected is not a function). Promise.prototype.then(onFulfilled, onRejected) -将实现和拒绝处理程序追加到Promise.prototype.then(onFulfilled, onRejected) ,并返回一个新的Promise.prototype.then(onFulfilled, onRejected)解析为被调用处理程序的返回值,或者如果Promise.prototype.then(onFulfilled, onRejected)则返回其原始结算值(即相关处理程序onFulfilled或onRejected不是函数)。 Below image explains thing : 下图说明了事情:

在此处输入图片说明

Read : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 阅读: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise

this is how it gose 这是怎么回事

-Promise - let this is ajax call,
  - then() - now this allows you to work on the output you received from first promise
     - then() - now if you want to work on the output of second promise which return by then(first) 
       - then - another promise allows to wok on ouput of third

so it kind of chaining ...go on ..till you complete it 所以有点链式...继续..直到您完成它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM