ES6 Promises-在Promise链中调用同步函数 [英] ES6 Promises - Calling synchronous functions within promise chain

查看:373
本文介绍了ES6 Promises-在Promise链中调用同步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试诺言,并且有一个非常基本的问题!

I'm currently experimenting with promises and have a really basic question!

在一个Promise链中,调用同步函数是一种不好的做法吗?例如:

Within a promise chain, would it be bad practice to call a synchronous function? For example:

.then(function(results) {

    if(checkIfResultInMemory(results) === true){
       return getTotalFromMemory()
    }

   return results;

  })

还是应该将我的同步功能重构为也返回承诺?

Or should my sync functions be refactored to return promises also?

推荐答案

在一个Promise链中,调用同步是一个不好的做法 功能?

Within a promise chain, would it be bad practice to call a synchronous function?

不,这根本不是一个坏习惯.这是许多预期和有用的实践之一.

No, it is not a bad practice at all. It is one of many expected and useful practices.

您完全可以自由地调用promise链中的同步函数(从.then()处理程序中)或异步函数,然后再返回新的promise.

You are perfectly free to call either synchronous functions within the promise chain (from within .then() handlers) or asynchronous functions that then return a new promise.

当您从.then()处理程序返回某些内容时,您可以返回一个值(它将成为父承诺的已解析值),也可以返回另一个承诺(链接到先前的承诺上),也可以抛出该承诺就像返回被拒绝的诺言(诺言链被拒绝)一样.

When you return something from a .then() handler, you can return either a value (which becomes the resolved value of the parent promise) or you can return another promise (which chains onto the previous promise) or you can throw which works like returning a rejected promise (the promise chain becomes rejected).

因此,这意味着您可以调用一个同步函数并从中获取一个值,或者调用一个异步函数并获取另一个诺言,然后从.then()处理程序中返回.

So, that means you can call a synchronous function and get a value from it or call an async function and get another promise and then return either from the .then() handler.

所有这些同步事物都是完全合法的,并且都有自己的目标.以下是.then()处理程序中的一些同步事件:

All of these synchronous things are perfectly legal and each have their own objective. Here are some synchronous happenings in the .then() handler:

// modify resolved value
someAsync().then(function(val) {
    return val + 12;
});

// modify resolved value by calling some synchronous function to process it
someAsync().then(function(val) {
    return someSynchronousFunction(val);
});

// synchronously check the value and throw to change the promise chain
// to rejected
someAsync().then(function(val) {
    if (val < 0) {
        throw new Error("value can't be less than zero");
    }
    return val;
});

// synchronously check the value and return a rejected promise 
// to change the promise chain to rejected
someAsync().then(function(val) {
    if (val < 0) {
        return Promise.reject("value can't be less than zero");
    }
    return val;
});


这是一个异步操作的小示例,该操作返回一个promise,后跟三个同步的.then()处理程序,然后输出最终值:


Here's a little example of an async operation that returns a promise followed by three synchronous .then() handlers and then outputting the final value:

function delay(t, val) {
    return new Promise(function(resolve) {
        setTimeout(function() {
            resolve(val);
        }, t);
    });
}

function increment5(val) {
    return val + 5;
}

delay(500, 10).then(increment5).then(function(val) {
    return val - 3;
}).then(function(final) {
    document.write(final);
});

注意:通常,您仅在有或可能有异步操作时才希望使用promise,因为如果一切都是同步的,那么纯同步代码既执行起来更快,编写起来也更容易.但是,如果您已经至少有一个异步操作,则可以将同步操作与该异步操作混合使用,并使用诺言来帮助构建代码.

Note: You only generally only want to use promises when you have or may have asynchronous operations because if everything is synchronous, then pure synchronous code is both faster to execute and easier to write. But, if you already have at least one async operation, you can certainly mix synchronous operations with that async operation and use promises to help structure the code.

这篇关于ES6 Promises-在Promise链中调用同步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆