[RxJS] Creation Operators:from, FromArray, frompromise

Source: Internet
Author: User

The of () operator essentially converted a list of arguments to an Observable. Since arrays is often how we structure the list of things in JavaScript, we should has a-to transforming arrays into Obs Ervables. This lesson teaches how can I convert from Arrays to observables, from Promises to Observables, and from iterators to Ob Servables.

Formarray:

varary = [1,2,3];varFoo =Rx.Observable.fromArray (ary); Foo.subscribe (function (x) {Console.log ('Next'+x);}, function (err) {Console.log ('Error'+err);}, function () {Console.log (' Done');});/*"Next 1" "Next 2" "Next 3" "Done"*/

Frompromise:

varProm = Fetch ('https://null.jsbin.com');varFoo =Rx.Observable.fromPromise (Prom); Foo.subscribe (function (x) {Console.log ('Next'+x.status);}, function (err) {Console.log ('Error'+err);}, function () {Console.log (' Done');});/*"Next 204 " "Done"*/

From

From () Opreator can-create observalbes according-to-you-passed in

=fromarray:

varary = [1,2,3];varFoo = rx.observable. from(ary); Foo.subscribe (function (x) {Console.log ('Next'+x);}, function (err) {Console.log ('Error'+err);}, function () {Console.log (' Done');});/*"Next 1" "Next 2" "Next 3" "Done"*/

=frompromiose

var foo = rx.observable.  from (Prom); Foo.subscribe (function (x) {  console.log (' + x.status);}, function (err) {  console.log (' + err);}, function () {  Console.log ('done');});

From iterator:

function*Generator () {yield Ten; yield  -; yield  -;}variterator =generator ();varFoo = rx.observable. from(iterator); Foo.subscribe (function (x) {Console.log ('Next'+x);}, function (err) {Console.log ('Error'+err);}, function () {Console.log (' Done');});/*"Next" "Next" "Next" "Done "*/

[RxJS] Creation operators:from, FromArray, frompromise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.