Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios with promise.prototype.finally doesn't work

Not sure what I am missing. Following the instructions here: https://github.com/mzabriskie/axios/blob/master/COOKBOOK.md

I have npm installed both axios and npm install axios promise.prototype.finally --save.

I am using Gulp with Browserify.

var axios = require('axios');
require('promise.prototype.finally');

axios.get('http://google.com').finally(function(){
    console.log('test');
});

Error:

app-7ee90adab7.js:18780 Uncaught TypeError: axios.get(...).finally is not a function

Update: This makes it work but do I need to do this?

var promiseFinally = require('promise.prototype.finally');
promiseFinally.shim();
like image 636
JC Lee Avatar asked Sep 26 '16 06:09

JC Lee


2 Answers

Try one of these see if it works:

  1. See if you have a global Promise available to shim it? Enter Promise in chrome console while the browserify is running the bundle. If you don't have it, use Babel or a lib that makes es6 or just Promise available.

  2. If it didn't work for some reason... well axios cookbook.md didn't get it right, since you have to call shim() to apply it on promise proto. why don't you use a shorthand require('promise.prototype.finally').shim(); if you don't like it that way, otherwise you have to require('es6-shim');

like image 103
Pouya Sanooei Avatar answered Oct 08 '22 05:10

Pouya Sanooei


FYI https://github.com/axios/axios/issues/34#issuecomment-558869934 Axios supports .finally().

like image 36
Andrii Omelianenko Avatar answered Oct 08 '22 04:10

Andrii Omelianenko