Last active
November 11, 2015 13:07
-
-
Save davideast/57250c6f2f761cedb0bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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