Skip to content
This repository was 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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/operator-docs/utility/toPromise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
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.'
},
{
type: 'Tip',
text: 'After calling toPromise you can make use of async/await!'
}
]
},
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']
};