GithubHelp home page GithubHelp logo

gianlucaguarini / allora Goto Github PK

View Code? Open in Web Editor NEW
677.0 16.0 17.0 27 KB

Promisify using es6 Proxies every javascript API with less than 50 lines of code

License: MIT License

JavaScript 100.00%

allora's Introduction

allora

Build Status

NPM version NPM downloads MIT License

Promisify everything in less than ~50 lines. It can be used to implement promises on any JavaScript object.

Examples

Basic

Or simply:

const w = allora(window)
w.onload.then(() => alert('I am ready!'))

Example in Node:

const allora = require('allora')
const g = allora(global)
Promise.all([
  g.setImmediate(),
  g.setTimeout(200),
  g.setTimeout(400)
]).then(() => done())

Example in browser:

const w = allora(window)
const p1 = w.setTimeout(500)
const p2 = w.setTimeout(5000)
const p3 = w.requestAnimationFrame()

p1.then(() => alert('I am ready'))
p2.then(() => alert('I am late'))
p3.then(() => alert('frame'))

Promise.all([p1, p2, p3]).then(() => alert('We are all done!'))

Clear timers

Thanks to this pull request it's now also possible to clear the timers

const w = allora(window)
const timer = w.setTimeout(3000)
timer.then(_ => console.log('time over'))
// the valueOf call should be not needed here
// but if you are on node, you will need it https://github.com/nodejs/node/issues/7792
clearTimeout(timer.valueOf())

Event streams

Allora does not allow you to do event streams since Promises could be only once fulfilled.

const w = allora(window)
const polling = w.setInterval(3000)
polling.then(() => console.log('This will be called only once!!!'))

However my friend @nilssolanki made stroxy to provide an elegant streaming api to any javascript object. I would highly recommend you stroxy if you are looking a lightweight events streaming library similar to allora.

"allora" meaning

"allora" is the Italian ๐Ÿ‡ฎ๐Ÿ‡น word for then

allora's People

Contributors

gianlucaguarini avatar impressivewebs avatar mohsen1 avatar nilssolanki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

allora's Issues

Regex problem

function isTimer (fn) {
  var name = fn.name === 'valueOf' ? fn.name : fn.toString()
  return /set[Timeout|Interval|Immediate]|requestAnimationFrame|timer/.test(name)
}

You want set(Timeout|Interval|Immediate), the above is equivalent to if you'd done: set[ITadeilmnortuv|], which would for example detect seti as the name of a timer function. You
should probably also use ^ and $ to terminate your regex. The two of these combined could cause, for example, a function called 'settleLedger' to match your isTimer check :)

Unexpected behavior on EventEmitters ".on" methods

Trying to use this on an EventEmitter

const EventEmitter = require('events');
const allora = require('allora')

const myEventEmitter = new EventEmitter();
const myPromisifiedEventEmitter = allora(myEventEmitter);

myPromisifiedEventEmitter.on('test').then((...args) => {
  console.log(args) // => [ [Function: t] ]
  console.log(args[0]()) // => undefined
})
myPromisifiedEventEmitter.emit('test', 'args[0] should be this text')

Getting this weird [ [Function: t] ] instead of 'args[0] should be this text'

repro https://github.com/laggingreflex/allora-test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.