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 3 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
48 changes: 46 additions & 2 deletions src/operator-docs/utility/toPromise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import { OperatorDoc } from '../operator.model';

export const toPromise: OperatorDoc = {
'name': 'toPromise',
'operatorType': 'utility'
name: 'toPromise',
operatorType: 'utility',
signature: 'public toPromise(PromiseCtor: *): Promise<T>',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's leave out generics in signature for now.

parameters: [
{
name: 'PromiseCtor',
type: '*',
attribute: 'optional',
description: `promise The constructor of the promise. If not provided,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you mean to have promise at the start of this sentence?

Copy link
Member Author

Choose a reason for hiding this comment

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

no I think it was a copy paste mistake :)

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: `
const source = Rx.Observable
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please change these to use es6 imports

.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']
};