简体   繁体   中英

create a promise chain

I need to create an object with user data.

  1. The first promise gives the user's location (city).

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)

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.)

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). 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. To build such scenarios, you can change the initial state of promises, making them rejected if necessary. The same data that is transmitted can also be changed, but we do not change the time in 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? Help please

Ciao, if you use Promise you can resolve or reject the promise result. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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