简体   繁体   中英

Change to async/await syntax

I have some code that print in console a number series start for 1, continuing for 2 and so on. I've done it with promises but now I want to change my promise script to async/await mode, but It doesn't work.

What I tried was this:

const alwaysThrows = () => {
    throw new Error("OH NOES");
};

const iterate = (integer) => {
    console.log(integer);
    return integer + 1;

};

const prom = Promise.resolve(iterate(1));



const manageOk = async () => {
    let result = await prom;
    console.log(result);



}

manageOk()


but I dont know how get the rest of numbers.

This is my original code:

const alwaysThrows = () => {
    throw new Error("OH NOES");
};

const iterate = (integer) => {
    console.log(integer);
    return integer + 1;

};

const prom = Promise.resolve(iterate(1));


prom
.then((value) => iterate(value))
.then(iterate)
.then(iterate)
.then(iterate)
.then(alwaysThrows)
.then(iterate)
.then(iterate)
.then(iterate)
.catch(e => console.log(e.message));

   const manageOk = async (val) => {
        return await iterate(val);
    }

    manageOk(1)
    .then(res => manageOk(res))
    .then(manageOk)
    .then(manageOk)
    .then(manageOk)

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