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

问题描述

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

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 函数的引用到 outside 承诺,那么你可以解决它在任何时候手动使用您想要的任何值.

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.

一个简单的例子 - 它仅用于演示目的:

A simplistic example - it only serves for demonstration purposes:

//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 调用,直到某些条件导致承诺解决.尝试输入你好"->添加到句子中 ->进入世界"->添加到句子中 ->造句

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 的解析函数分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:40