diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts index a1b2cbc1a..1be9f8508 100644 --- a/src/plugins/plugin.ts +++ b/src/plugins/plugin.ts @@ -51,7 +51,13 @@ function setIndex(args: any[], opts: any = {}, resolve?: Function, reject?: Func } else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') { // If we've specified a success/error index args.splice(opts.successIndex, 0, resolve); - args.splice(opts.errorIndex, 0, reject); + + // We don't want that the reject cb gets spliced into the position of an optional argument that has not been defined and thus causing non expected behaviour. + if (opts.errorIndex > args.length) { + args[opts.errorIndex] = reject; //insert the reject fn at the correct specific index + } else { + args.splice(opts.errorIndex, 0, reject); //otherwise just splice it into the array + } } else { // Otherwise, let's tack them on to the end of the argument list // which is 90% of cases