1.0.1 • Published 7 years ago

asynclock v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

asynclock

A helper for pausing functions.

Example Usage

const asynclock = require("asynclock")
const al = asynclock();

const send = (data) => {
    const Random = 123; // Generate Random ID
    const Pack = JSON.stringify({id: Random, data}); // Pack Data with Id
    // Send Data Package 
    // Socket.send(Pack);
    
    setTimeout(function(){onmessage(Pack);}, 1000); // For simulating response 
    
    // Return Lock
    return al.Lock(Random);
}

const onmessage = (Pack)=> {
    const Unpacked = JSON.parse(Pack);
    al.UnLock(Unpacked.id, Unpacked.data);
}

const test = async function(){
    const retValue = await send({query: "getUsers"});
    console.log("retValue", retValue);
}

test();

Library

{
    Lock: function(Id){}, // (Promise) call when you need to pause
    UnLock: function(Id, Value){} // (undefined) call when you need to continue
}

Id can be any javascript object key.