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

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

问题描述

我目前正在试验 Promise 并且有一个非常基本的问题!

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

在承诺链中,调用同步函数是不好的做法吗?例如:

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;

  })

或者应该重构我的同步函数以返回 promise 吗?

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.

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

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,因为如果一切都是同步的,那么纯同步代码执行起来更快,编写起来也更容易.但是,如果您已经至少有一个异步操作,那么您当然可以将同步操作与该异步操作混合使用,并使用 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天全站免登陆