Created
June 21, 2018 12:06
-
-
Save huytrongnguyen/9330bdcc93ba35aa80a63139324c229a to your computer and use it in GitHub Desktop.
Ajax promise with Observable.ajax
This file contains 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
import { Observable } from 'rxjs'; | |
class Ajax { | |
request({ url, method = 'get', responseType = 'json', params }) { | |
return new Promise((resolve, reject) => { | |
Observable.ajax({ | |
url, | |
method, | |
headers: { 'Content-Type': 'application/json' }, | |
body: method === 'post' && params, | |
responseType, | |
}) | |
.subscribe({ | |
next: value => resolve(value.response), | |
error: err => reject(err.message || err.response), | |
}); | |
}); | |
} | |
} | |
export default new Ajax(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment