Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

ogen

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ogen

An observable Async/Await. Write async code that looks synchronous.

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

Ogen

An observable Async/Await. Short for (O)bservable (Gen)erator.

Write asynchronous code that looks synchronous:

const myFunc = function* (param1, param2, param3) {
  const result = yield fetchSomething(); // returns promise

  // waits for promise and uses promise result
  yield result + ' 2';
  yield param1;
  yield param2;
  yield param3;
}

Pass it into ogen() and get back an observable that lets you subscribe to all the yielded values:

const onNext = val => console.log(val);
const onError = err => console.log(err);
const onComplete = () => console.log('done.');

const asyncFunc = ogen(myFunc);

// Call the async function and pass params.
asyncFunc('a param', 'another param', 'more params!')
  .subscribe(onNext, onError, onComplete);
// future value
// future value 2
// a param
// another param
// more params!
// done.

Ogen returns a full Rx Observable instance, which means you can .map(), .filter() and .skip() to your heart’s content, among other things.

Platform Notes

Obviously, this relies on generators. Works OK with Babel, Node v4+. Does not work in any IE without polyfills.

Should work in most other modern browsers.

Written for Learn JavaScript with Eric Elliott

eejs-screenshot

Join the chat at https://gitter.im/learn-javascript-courses/javascript-questions

An online course series for application developers. Ready to jump in? Learn more.

Keywords

async

FAQs

Package last updated on 13 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts