将Promise的resolve函数分配给变量 [英] Assign resolve function from Promise to a variable

查看:53
本文介绍了将Promise的resolve函数分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一小段我似乎无法理解的代码.首先有一个函数声明为空:

There's a snippet of code that I cannot seem to understand. There's a function that is declare empty at first:

let dCredentials = (credentials: any) => {}

然后,此变量在稍后称为:

Then this variable is later reasignned in a function later called:

  return new Promise((resolve, _) => (dispatchCredentials = resolve))

第二个代码做什么?有人在诺言中使用.then后,它会返回任何内容吗?

What does the second code do? Does it return anything after someone uses .then in the promise?

推荐答案

如果您引用了 resolve 函数以将承诺超出,则可以解决该问题随时随地使用您想要的任何值.

If you get a reference to the resolve function to outside the promise, then you can resolve it at any point manually with any value you want.

一个简单的例子:

//create a variable to use for the resolve function
let outsideResolve;

//make a promise
const p = new Promise((resolve) => outsideResolve = resolve);

//attach callbacks
p
  .then(value => value.toUpperCase())
  .then(value => value + "!")
  .then(value => console.log("Final value:", value));

//add a click listener that will be invoked at any point in time
document.getElementById("click_me").addEventListener("click", () => {
  const input = document.getElementById("fill_me");
  
  //resolve the promise once clicked
  outsideResolve(input.value);
})

<input type="text" id="fill_me"/>

<button id="click_me">resolve with value</button>

进行解析

您仍然可以照常使用Promise,但是您可以更好地控制何时以及如何解决该问题.

You can still use the Promise as normal but you have more control over when and how it's resolved.

如果您知道要执行什么操作,但是不能说何时需要应用这些操作(这将有助于稍后处理数据),则这可能会有所帮助.

This can be helpful if you know what operations you want to do but cannot say when they need to be applied as the data (and thus resolution) will come later.

此外,您可能必须不确定性地添加越来越多的 .then 调用,直到某些情况导致promise解决.尝试输入你好"->添加到句子->输入世界"->添加到句子->构建句子

In addition, you might have to add more and more .then calls indeterminably until some condition causes the promise to resolve. Try entering "hello" -> add to sentence -> enter "world" -> add to sentence -> build sentence

//create a variable to use for the resolve function
let outsideResolve;

//make a promise
let p = new Promise((resolve) => outsideResolve = resolve);

//add a click listener that will be invoked at any point in time
document.getElementById("add_me").addEventListener("click", () => {
  const input = document.getElementById("fill_me");
  
  const inputValue = document.getElementById("fill_me").value;
  
  p = p.then(value => `${value} ${inputValue}`);
  console.log(`"${inputValue}" added.`)
  
  input.value = "";
})

//add a click listener that will be invoked at any point in time
document.getElementById("click_me").addEventListener("click", () => {
  //attach final callbacks
  p
    .then(value => value.toUpperCase())
    .then(value => value + "!")
    .then(value => console.log("Final value:", value));

  //resolve the promise once clicked
  outsideResolve("start resolving");
})

<input type="text" id="fill_me"/>

<button id="add_me">add to sentence</button>
<button id="click_me">build sentence</button>

这篇关于将Promise的resolve函数分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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