npm.io
0.0.6 • Published 11 years ago

promise-lock

Licence
WTFPL
Version
0.0.6
Deps
0
Vulns
0
Weekly
0

promise-lock

NPM Version Build Status

Installation

npm install promise-lock

Usage

  1. Rejecting concurrent promises
    const lock = new PromiseLock();

    lock.on($.get('/foo'))
        .then((res)=>console.log(res))       //<-- will log the result
        .catch((res)=>console.log('Error'));

    lock.on($.get('/bar'))
        .then((res)=>console.log(res))
        .catch((res)=>console.log('Error'));//<-- will log "Error", because the lock is previously set on anothe promise
  1. Cancelling previously started promise
    const lock = new PromiseLock();

    lock.on($.get('/foo'))
        .then((res)=>console.log(res))       //<-- will never run
        .catch((res)=>console.log('Error')); //<-- will never run
    
    lock.cancel();

    lock.on($.get('/bar'))
        .then((res)=>console.log(res))       //<-- will log the result
        .catch((res)=>console.log('Error'));