1.0.7 • Published 6 years ago

awaitor v1.0.7

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

Awaitor

await task among several async functions.

Usage

const Awaitor = require('awaitor');
const sleep = ms => new Promise(r => setTimeout(r, ms));

let question1 = 'who is your favorite singer?';
let question2 = 'why?';
let question3 = 'how old are you?';
let question4 = 'where are you come from?';

let awaitor = new Awaitor();

(async () => {
    let answer1 = await awaitor.wait(question1);
    console.log(question1, answer1);
    let answer2 = await awaitor.wait(question2);
    console.log(question2, answer2);
    await sleep(800);
    let answer3 = await awaitor.wait(question3).catch(err => console.log(`caught an error: ${err}`));
})()
.catch(console.error);


(async () => {
    await sleep(200);
    awaitor.resolve(question1, 'pg lost');
    await sleep(200);
    awaitor.resolve(question2, 'I like post-rock');
    awaitor.reject(question3, 'I dont want to tell you');

    // re-resolving or re-rejecting an task will be ignored
    awaitor.resolve(question3, 'I am kiding');

    // resovling/rejecting a question that no one is waiting for it now.
    // It will just go silent, and take effect immediately as soon as someone wait for it.
    awaitor.reject(question4, 'I dont want to tell you');
})()
.catch(console.error);
1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago