0.0.10 • Published 4 years ago

synchronous-async v0.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Synchronous Async

npm GitHub top language NPM

GitHub last commit

paypal

Warning: package depreciated. Use async-extra instead

This may sound confusing, but this npm module runs multiple async functions, and returns them in sync. While this does need to run inside async/await functions, it can be useful in a few situations.

Imagine you need to run many large processes at the same time, but the results are dependent on each other. With this module, you could start to run all those functions at the top of your own async function, do some other stuff while those functions are running in the background, and then, when ready, grab all the results in the same order you ran them in.

Installation

npm install synchronous-async

Setup

const syncAsync = require('synchronous-async');

Usage

async function myFunction(){
    
    let options = {
        timeout: 3000,
        checkInterval: 10,
    };
    
    let operation = syncAsync.function(options, myLongProcess1, myLongProcess2, myLongProcess3);
    
    // do stuff...
    
    await operation.onFinish((result, index, results, finished) => {
        // onFinish loops through each result, and runs this callback.
        // "result" is the result of the current callback.
        // "index" is the index of the callback (which matches the order you put your functions in).
        // "results" is the array of results (in order) of all the functions.
        // finished returns true or false. If timeout runs before all processes are finished, this returns false.
        // you can run this function again, if finished === false.
    });

    // an alternative to onFinish
    let result = await operation.result();
    console.log(result); // output: {results: [resultArray], finished: true || false, unfinished: number of processes left}
    
    // if you need some results as soon as possible
    result = operation.resultSync();
    console.log(result); // output: same as result(), but a much higher chance for empty results.
    if(result[0]){
        //first process finished
    }
    
    // to simply try and wait for results to finish
    await operation.wait();

    // you can also change the timeout at any time
    operation.setOptions({timeout: 3000, checkInterval: 5});
    // timeout is the number of milliseconds before the function gives up waiting for a result.
    // checkInterval is the number of milliseconds to wait between checking if the result is there yet.
}

// other methods

// run an async forEach loop, and wait for it to finish
await syncAsync.forEach(objectOrArray, callback);
// output: {finished: true || false, unfinished: number of items left}

// run an async map loop, and wait for the results, and if an array, in the same order they were originally in
await syncAsync.map(objectOrArray, callback, {timeout: 5000, checkInterval: 5});
// output: {result: [resultArray], finished: true || false, unfinished: number of items left}

// make an async function sleep for a number of milliseconds
await syncAsync.sleep(ms);

// the function this module uses to wait for results from async operations
let value = false;
// set value in async function...
await syncAsync.waitForValue(() => value, timeout, checkInterval);
0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago