Javascript:如何确定是否承诺功能? [英] Javascript: How to determine whether to promisefy a function?

查看:97
本文介绍了Javascript:如何确定是否承诺功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一些小的辅助函数,其中一些显然是异步/诺言的候选者(据我所知):

Consider small helper functions, some of which are obvious candidates for async / promise (as I think I understand them):

exports.function processFile(file){
  return new Promise(resolve => {
    // super long processing of file
    resolve(file);
  });
}

与此同时:

exports.function addOne(number){
  return new Promise(resolve => {
    resolve(number + 1);
  });
}

似乎矫kill过正.

确定他们是否应履行其职责的规则是什么?

What is the rule by which one determines whether they should promise-fy their functions?

推荐答案

确定他们是否应履行其职责的规则是什么?

What is the rule by which one determines whether they should promise-fy their functions?

实际上很简单:

  • 该函数执行异步操作时,应返回对结果的承诺
  • 当函数仅执行同步操作时,它不应返回promise

这篇关于Javascript:如何确定是否承诺功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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