简体   繁体   English

创建 promise 链

[英]create a promise chain

I need to create an object with user data.我需要用用户数据创建一个 object。

  1. The first promise gives the user's location (city).第一个 promise 给出了用户的位置(城市)。

2.The second is his role, and depending on the role, you need to compose an email for him (if the user is admin - email should end with admin.com and similarly with other roles) 2.第二个是他的角色,根据角色,你需要为他编写一个email(如果用户是admin - email应该以admin.Z4D236D9A2D102C5FE6AD1C50DA4BEC5结尾)和其他角色类似

3.Further, if the user is admin, you need to call the function, passing the user id into it and, depending on the id, return a number that will indicate the number of people in his group (for this function, you can use a promise, you can think of the id match to the number of people yourself, it can be an object with a key id and a value as a person. {1: 3, 2: 5}, etc.) 3.此外,如果用户是管理员,则需要调用 function,将用户 id 传入其中,并根据 id 返回一个数字,该数字将指示其组中的人数(对于此 function,您可以用一个promise,你可以自己想id和人数匹配,可以是object,key为id,值为person。{1:3, 2:5}等)

4.In total, if the user is on the admin, and the chain has come to ignore the list of people in the group, only throw an error using throw new Error (error text). 4.总的来说,如果用户是管理员,并且链已经开始忽略组中的人员列表,则仅使用 throw new Error(错误文本)抛出错误。 If the user is admin, but the number of people in the group has come more than indicated, throw the corresponding error, the chain of promins should end with an error that makes it clear what happened.如果用户是管理员,但组中的人数超过了指示,则抛出相应的错误,promins 链应该以一个错误结束,说明发生了什么。 To build such scenarios, you can change the initial state of promises, making them rejected if necessary.要构建此类场景,您可以更改 Promise 的初始 state,如有必要,将其拒绝。 The same data that is transmitted can also be changed, but we do not change the time in setTimeout.传输的相同数据也可以更改,但我们不更改 setTimeout 中的时间。

const promise1 = new Promise(resolve) => {
  setTimeout(() => Promise.resolve('Madrid'), 1000); // location
} 
const promise2 = new Promise(resolve) => {
  setTimeout(() => Promise.resolve('admin'), 4000); // three role - guest, user, admin
} 
const promise3 = new Promise(resolve) => {
  setTimeout(() => Promise.resolve('1'), 2000); // id user, number can be any
}
const promise4 = new Promise(resolve) => {
  setTimeout(() => Promise.resolve(['Alex', 'Vlad', 'Marta', 'Alexandr', 'Stefan']), 1000); // an array that shows the people in a person's group
}

It is necessary to rewrite this in view of the chain of promises it's my solution鉴于承诺链,有必要重写这是我的解决方案

let promise = new Promise((resolve) => {
    setTimeout(() => resolve(city), 1000);
}).then((role) => {
    if(role != 'admin') {
        throw new Error('not admin')
    }
    return new Promise((resolve) => { 
        setTimeout(() => resolve(admin), 4000);
        })
    }).then((id) => {
        return new Promise((resolve) => {
            setTimeout(() => Promise.resolve(id), 2000); 
        })
    }).then((people) => {
        return new Promise((resolve) => {
                setTimeout(() => Promise.resolve(people), 1000); 
            })
    }) 

But how to compose an email and display the necessary errors with the role and number of people and finally return object right?但是如何编写一个 email 并显示必要的错误以及角色和人数,最后返回 object 对吗? Help please请帮忙

Ciao, if you use Promise you can resolve or reject the promise result. Ciao,如果您使用 Promise,您可以resolvereject promise 结果。 Something like:就像是:

const promise1 = () => { 
  return new Promise(resolve, reject) => {
     if (/*condition*/) setTimeout(() => resolve('Madrid'), 1000);
     else reject("this is an error");
  } 
}

Using this pattern, you could rewrite you promise chain like this:使用这种模式,您可以像这样重写 promise 链:

promise1()
   .then((result) => {
       // here result = "Madrid" 
       // well lets call the second promise promise2   
    })
    .catch((error) => {
       //here error = "this is an error"
       // manage the error
    });

In this way you can easly manage promises results or errors.通过这种方式,您可以轻松管理 promise 结果或错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM