以上就是给各位分享为什么.then,其中也会对中的value未定义链接到Promise?进行解释,同时本文还将给你拓展axios在ie浏览器下提示promise未定义、bluebird,promisi
以上就是给各位分享为什么.then,其中也会对中的value未定义链接到Promise?进行解释,同时本文还将给你拓展axios在ie浏览器下提示promise未定义、bluebird,promisify和then()、ie 浏览器 vuejs axios Promise 未定义、javascript – AngularJS $promise then()数据未定义等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- 为什么.then()中的value未定义链接到Promise?(the value of is not usable)
- axios在ie浏览器下提示promise未定义
- bluebird,promisify和then()
- ie 浏览器 vuejs axios Promise 未定义
- javascript – AngularJS $promise then()数据未定义
为什么.then()中的value未定义链接到Promise?(the value of is not usable)
给定
function doStuff(n /* `n` is expected to be a positive number */) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(n * 10) }, Math.floor(Math.random() * 1000)) }) .then(function(result) { if (result > 100) { console.log(result + " is greater than 100") } else { console.log(result + " is not greater than 100"); } })}doStuff(9).then(function(data) { console.log(data) // `undefined`, why?})
为什么data
undefined
在.then()
链接到doStuff()
电话吗?
答案1
小编典典因为没有ed Promise
或其他值return
从.then()
链接到Promise
构造函数。
请注意,.then()
将返回一个新Promise
对象。
解决方案是return
使用return
value或Promise
from 的值或其他函数调用.then()
。
function doStuff(n /* `n` is expected to be a positive number */) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(n * 10) }, Math.floor(Math.random() * 1000)) }) .then(function(result) { if (result > 100) { console.log(result + " is greater than 100") } else { console.log(result + " is not greater than 100"); } // `return` `result` or other value here // to avoid `undefined` at chained `.then()` return result })}doStuff(9).then(function(data) { console.log("data is: " + data) // `data` is not `undefined`});
axios在ie浏览器下提示promise未定义
最近修改项目的兼容性,项目测试ie11兼容性时发现axios会提示promise未定义,在chrome下完全没毛病,怎么去到ie11就报promise未定义了呢?
翻了一轮axios的文档终于发现问题原因。
在ie11下直接使用axios会报Promise未定义,这是因为axios本质上是封装了ES6语法的promise,而promise在IE上并不兼容。
这时只需要引入babel-pollfile就行了。babel-polyfill相当于一个ES6的兼容垫片, 它会仿效一个完整的ES2015+环境,从而让某些浏览器也能读懂。
npm install --save babel-polyfill
然后在main.js的顶部引入import “babel-polyfill”; 最好给它最优先加载。
随后在build目录下webpack.config.js(如果你是vue-cli项目的话那就是build目录下webpack.base.conf.js)设置app载入babel-polyfill。
module.exports = {
entry: {
app: ["babel-polyfill",'./src/main.js']
}
}
添加完之后,刷新一下浏览器,promise未定义就消失啦!
bluebird,promisify和then()
我只使用了bluebird几天,但是我想遍历所有旧代码及其promisify
:)
我的问题是我仍然没有完全掌握then()
命令的流程。
考虑以下两个块:
一个
methodThatReturnsAPromise().then(task2).then(task3);
乙
var promise = methodThatReturnsAPromise();
promise.then(task2)
promise.then(task3);
-
在方案A
task3
中将得到的结果task2
?在B中,他们都得到了第一个承诺的结果? -
第二个与
Promise.all
从bluebird 运行有何不同? -
这些A / B /
Promise.all
在使用catch
方法方面有何不同(我在哪里放置它)。
抱歉,这是一堆问题。
ie 浏览器 vuejs axios Promise 未定义
随着前端技术的发现,es6 语法在被更大范围的使用,而很多的浏览器并不支持 ES6,比如 IE…… 这里我们介绍几个解决方法。
一、使浏览器兼容 ES6 基本语法
1、在引入其他脚本前先引入 browser.min.js。(脚本下载地址:开发版本 browser.js;生产版本 browser.min.js。)
2、script 标签的 type 的值设为 text/babel。
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript" src="./babel/browser.min.js"></script>
<script type="text/babel">
const list = [''one'',''two'',''three''];
list.forEach( (item,index) => {
alert(item + (index+1));
});
</script>
</body>
</html>
二、使 IE 浏览器兼容 ES6(Promise 等语法)
Babel 默认只转换新的 JavaScript 句法,而不转换新的 API ,比如 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象,以及一些定义在全局对象上的方法(比如 Object.assign)都不会转码。为了解决这个问题,我们使用一种叫做 Polyfill(代码填充,也可译作兼容性补丁) 的技术。
在页面引入 browser-polyfill 脚本:
引入 <script src = "https://cdn.polyfill.io/v2/polyfill.min.js"></script> 或 <script type="text/javascript" src ="https://cdn.polyfill.io/v2/polyfill.min.js?features=es6"></script>
javascript – AngularJS $promise then()数据未定义
angular.module('testSiteApp').controller('TestController',function ($scope,Tests) { $scope.test = Tests.get({id: 1}); $scope.test.$promise.then(function(data) { $scope.tasks = data.tasks; console.log($scope.tasks); }); console.log($scope.tasks); });
then()函数内的结果:
[Object,Object,Object]
then()函数之外的结果:
undefined
我正在使用的’Tests’服务工厂如下:
angular.module('testSiteApp').factory('Tests',function($resource) { return $resource('/api/test/:id',{id: '@id'},{ 'update': { method: 'PUT' } } ); });
即使我使用查询方法而不是get for my资源并将isArray设置为true,我仍然会遇到同样的问题.由于某种原因,数据没有绑定到then函数中的我的范围.
我很抱歉,如果这是一个重复的问题,但我到处寻找,只发现与$promise函数有关的未定义问题,在这种情况下不是问题.
在此先感谢您的支持.
解决方法
考虑时间(当然时间只是一个例子):
// time = 0.000 sec. You make a request to the backend $scope.test = Tests.get({id: 1}); $scope.test.$promise.then(function(data) { // time = 1.000 sec. Request is completed. // data is available,so you assign it to $scope.tasks $scope.tasks = data.tasks; console.log($scope.tasks); }); // time = 0.000 sec (!!!) This has been called NOT AFTER // the callback,but rather immediately after the Tests.get() // So the data is not available here yet. console.log($scope.tasks);
今天的关于为什么.then和中的value未定义链接到Promise?的分享已经结束,谢谢您的关注,如果想了解更多关于axios在ie浏览器下提示promise未定义、bluebird,promisify和then()、ie 浏览器 vuejs axios Promise 未定义、javascript – AngularJS $promise then()数据未定义的相关知识,请在本站进行查询。
本文标签: