Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

docs(operators): add documentation for toPromise #228

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
49 changes: 47 additions & 2 deletions src/operator-docs/utility/toPromise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
import { OperatorDoc } from '../operator.model';

export const toPromise: OperatorDoc = {
'name': 'toPromise',
'operatorType': 'utility'
name: 'toPromise',
operatorType: 'utility',
signature: 'public toPromise(PromiseCtor: *): Promise',
parameters: [
{
name: 'PromiseCtor',
type: '*',
attribute: 'optional',
description: `The constructor of the promise. If not provided,
it will look for a constructor first in Rx.config.Promise then fall back to the native Promise constructor if available.`
}
],
shortDescription: {
description:
'Converts an Observable sequence to a ES2015 compliant promise.'
},
walkthrough: {
description: `An ES2015 compliant promise which contains the last value from the Observable sequence.
If the Observable sequence is in error, then the Promise will be in the rejected stage.
If the sequence is empty, the Promise will not resolve.`,
extras: [
{
type: 'Tip',
text:
'This operator makes reactive programming easy to use for developers who are not used to it.'
}
]
},
examples: [
{
name: 'Just return the emitted value of the observable as a promise',
code: `
import {of} from 'rxjs/observable/of';

const source = of(42)
.toPromise();

source.then((value) => console.log('Value: %s', value));
// => Value: 42
`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this example.

it is based on https://www.learnrxjs.io/operators/utility/topromise.html

//return basic observable
const sample = val => Rx.Observable.of(val).delay(5000);
/*
  convert each to promise and use Promise.all
  to wait for all to resolve.
Maybe is a little too fancy (await and destructuring assignment).
*/
const [promise1, promise2] = await Promise.all([
    sample('Promise 1').toPromise(),
    sample('Promise 2').toPromise()
  ]);

//output: "Promise 1", "Promise 2"
promise1().then(val => {
  console.log('Promise.all Result:', val);
});
promise2().then(val => {
  console.log('Promise.all Result:', val);
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now I would just add a hint. If we use stackblitz one could add a proper example with async await

externalLink: {
platform: 'JSBin',
url: 'http://jsbin.com/fanivejahe/embed?js,console,output'
}
}
],
relatedOperators: ['fromPromise']
};