Skip to content

Instantly share code, notes, and snippets.

@superherointj
Last active December 14, 2015 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superherointj/efbd2063a7048ec6b1d4 to your computer and use it in GitHub Desktop.
Save superherointj/efbd2063a7048ec6b1d4 to your computer and use it in GitHub Desktop.
/*
Thank you for helping.
I am trying to figure out how RxJS works.
I'm really new to this and haven't made 'the leap' yet.
I appreciate your feedback.
Question:
Is it good idea to use RxJS to replace 'callbacks' in a routine?
I'm after a solution for 'callback hell'.
And I've read somewhere RxJS is a replacement for Promises.
Problem:
=> How is possible to execute the following routine in sequence (like synchronously would do)
but without blocking the thread just 'queueing' one operation after the other?
*/
var Rx = require('rx');
const evSequence = [
{ order: 1, timer: 3 },
{ order: 2, timer: 2 },
{ order: 3, timer: 4 },
{ order: 4, timer: 8 },
{ order: 5, timer: 9 },
{ order: 6, timer: 1 },
{ order: 7, timer: 6 },
{ order: 8, timer: 5 },
{ order: 9, timer: 7 },
];
var returnTimeout$ = Rx.Observable.from(evSequence);
returnTimeout$.subscribe(function (item){
var o = Rx.Observable.just(item).delay(new Date(Date.now() + item.timer*1000));
o.subscribe(item=> {
console.log(item.order, item.timer);
});
});
/*
Current output will take only 9 seconds to process and it will be:
c:\Dev\Node\rx\tl1>node rxAsyncDemo.js
6 1
2 2
1 3
3 4
8 5
7 6
9 7
4 8
5 9
Desired Output: should take 45 seconds and be:
1 3
2 2
3 4
4 8
5 9
6 1
7 6
8 5
9 7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment