Skip to content

Instantly share code, notes, and snippets.

@davideast
Last active November 11, 2015 13:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save davideast/57250c6f2f761cedb0bb to your computer and use it in GitHub Desktop.
interface IFetchResponse {
text: () => Promise<string>;
json: () => Promise<string>;
}
declare var fetch: (url: string) => Promise<IFetchResponse>;
@Pipe({
name: 'fetch'
})
class FetchJsonPipe {
private fetchedValue:any;
private fetchPromise:Promise<any>;
transform(value:string, args:string[]):any {
if (!this.fetchPromise) {
this.fetchPromise = fetch(value)
.then(result => result.json())
.then(json => {
this.fetchedValue = json;
});
}
return this.fetchedValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment