Skip to content

Instantly share code, notes, and snippets.

@huytrongnguyen
Created June 21, 2018 12:06
Show Gist options
  • Save huytrongnguyen/9330bdcc93ba35aa80a63139324c229a to your computer and use it in GitHub Desktop.
Save huytrongnguyen/9330bdcc93ba35aa80a63139324c229a to your computer and use it in GitHub Desktop.
Ajax promise with Observable.ajax
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