静修-个人博客


  • 首页

  • 分类

  • 归档

  • 标签

  • 搜索

73-promise注意小点

发表于 2019-03-28 | 分类于 前端-02-js基础复习

1.调用resolve或reject并不会终结 Promise 的参数函数的执行

注意,调用resolve或reject并不会终结 Promise 的参数函数的执行。

1
2
3
4
5
6
7
8
new Promise((resolve, reject) => {
resolve(1);
console.log(2);
}).then(r => {
console.log(r);
});
// 2
// 1

上面代码中,调用resolve(1)以后,后面的console.log(2)还是会执行,并且会首先打印出来。这是因为立即 resolved 的 Promise 是在本轮事件循环的末尾执行,总是晚于本轮循环的同步任务。

一般来说,调用resolve或reject以后,Promise 的使命就完成了,后继操作应该放到then方法里面,而不应该直接写在resolve或reject的后面。所以,最好在它们前面加上return语句,这样就不会有意外。

2.报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
window.onerror = handleError
function handleError(msg,url,l){
  var txt="There was an error on this page.\n\n"
    txt+="Error: " + msg + "\n"
    txt+="URL: " + url + "\n"
    txt+="Line: " + l + "\n\n"
    txt+="Click OK to continue.\n\n"
console.log(txt)
    return false
} //如果返回值为 false,则在控制台 (JavaScript console) 中显示错误消息。反之则不会

// window.addEventListener('unhandledrejection', event => {
// console.log("捕获到promise全局错误");
// console.log(event)
// });

function a(){
return new Promise((resolve,reject)=>{
reject("呜呜")
})
}
function b(){
return new Promise((resolve,reject)=>{
resolve("哈哈")
})
}

async function c(){
const r1= await a().catch(err=>{throw err});
const r2= await b();
}

c()

在Promise报错(无论reject还是throw)都会被window.addEventListener('unhandledrejection', event => {}接收到,window.onerror监听不到

window.onerror只能捕获全局异常

  • csdn
  • 如何优雅处理前端异常?

3. 中断或取消Promise链的可行方案

当新对象保持“pending”状态时,原Promise链将会中止执行。

1
2
3
4
5
6
7
8
9
10
11
12
Promise.resolve()
.then(() => {
console.log('[onFulfilled_1]');
return new Promise(()=>{}); // 返回“pending”状态的Promise对象
})
.then(() => { // 后续的函数不会被调用
console.log('[onFulfilled_2]');
})
.catch(err => {
console.log('[catch]', err);
});
// => [onFulfilled_1]

4 面试题相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
console.log(1)

setTimeout(function() {
console.log(2)

new Promise(function(resolve) {
console.log(3)
resolve(4)
}).then(function(num) {
console.log(num)
})
}, 300)

new Promise(function(resolve) {
console.log(5)
resolve(6)
}).then(function(num) {
console.log(num)
})

setTimeout(function() {
console.log(7)
}, 400)

91-babel转义import

发表于 2019-03-28 | 分类于 前端-02-js基础复习

fu()和(0,fu)()有什么区别

1
2
3
4
5
import { a } from 'b';

function x () {
a()
}

babel转成以下代码:

1
2
3
4
5
6
7
'use strict';

var _b = require('b');

function x() {
(0, _b.a)();
}

(0, _b.a)() ensures that the function _b.a is called with this set to the global object (or if strict mode is enabled, to undefined).

(0, _b.a)()确保 function _b.a this 绑定到当前全局变量,如果是在严格模式,则绑定到undefined。 如果你直接使用_b.a,则this指向的是_b

27-小技巧

发表于 2019-03-27 | 分类于 前端-08-vue

1.获取数据前,页面显示问题

  • 获取数据前:可以用loading显示等待获取
  • 获取数据后:如果数据为空,提示“内容为空”
1
<div class="yx-center tip" v-if="form&&!form.length">
1
2
3
4
5
data() {
eturn {
form: '' // 初始化为 false 或者 空值,初始化值类型应该与数据类型不一致,避免页面进来的时候就显示“内容为空”的提示
}
},

89-设置概率问题

发表于 2019-03-27 | 分类于 前端-02-js基础复习

我需要设置出现1~5的概率为55% 5~10的概率40% 10~15的概率5%

1
2
3
4
5
6
7
8
9
var ran=Math.ceil(Math.random()*100)

if(ran<55){ //通过概率判断选取数字
var needNum= Math.ceil(Math.random()*5) //选取需求数字
}else if(55<ran<95){
var needNum= Math.ceil(Math.random()*5+5)
}else if(95<ran<100){
var needNum= Math.ceil(Math.random()*5+10)
}

13-自定义服务

发表于 2019-03-27 | 分类于 前端-06-Angular

博客园
自定义指令
AngularJs指令配置参数scope详解

14-ng零散知识点

发表于 2019-03-27 | 分类于 前端-06-Angular

1. $new

要初始化一个控制器实例,需要使用$new()方法从$rootScope创建某作用域的一个新实例。
这个新实例会建立Angular在运行时使用的作用域继承。
有了这个作用域,就可以初始化一个新的控制器,把这个作用域作为控制器的$scope传递
过去。

2. $q

参考链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var HttpREST = angular.module('Async',[]);

//defer.resolve(),defer.reject(),defer.notify()
HttpREST.controller('promise',function($q,$http,$scope){
var defer = $q.defer(); //创建了一个defer对象;

var promise = defer.promise; //创建了defer对象对应的promise

promise.then(function(data){$scope.name='成功'+data},function(data){$scope.name='失败'+data},function(data){$scope.name='进度'+data});

$http({
method:'GET',
url:'/name'
}).then(function(res){
defer.resolve(res.data)
},function(res){
defer.reject(res.data)
})
});

09-在create-react-app中安装mobx

发表于 2019-03-27 | 分类于 前端-05-react

中文文档

1. 执行npm run eject

2. 启用装饰器语法

  • 如果是TypeScript环境

在 tsconfig.json 中启用编译器选项 "experimentalDecorators": true 。

  • 如果是js环境

新建jsconfig.json

1
2
3
4
5
6
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}

接下来,执行

1
npm install --save-dev babel-preset-mobx

在package.json babel字段增加 "mobx"

1
2
3
4
5
6
  "babel": {
"presets": [
"react-app"
++++ "mobx"
]
},

87-作用域、执行上下文、变量对象、作用域链

发表于 2019-03-27 | 分类于 前端-02-js基础复习
  • 作用域:规定了如何查找变量,也就是确定当前执行代码对变量的访问权限。
    JavaScript 采用词法作用域,也就是静态作用域,在定义时就确定了作用域。

  • 执行上下文:是全局或函数的执行环境,js引擎使用执行上下文栈来管理执行上下文。

    • 初始化的时候首先就会向执行上下文栈压入一个全局执行上下文
    • 当执行到函数时,就将函数的执行上下文压入栈中。函数执行完毕后就会将函数的执行上下文从栈中弹出。

每个执行上下文都有以下三个属性:

  • 变量对象:存储了上下文中定义的变量和函数声明
    在函数定义阶段是变量对象;在执行阶段是活动对象。

  • 作用域链:作用域链是保证执行上下文有权访问的所有变量和函数的有序访问。
    前端是当前函数的变量对象,然后是父级上下文的变量对象,最后是全局变量对象。

  • this

15-双冒号的作用

发表于 2019-03-27 | 分类于 前端-06-Angular

:: is used for one-time binding. The expression will stop recalculating once they are stable, i.e. after the first digest.

So any updates made to something will not be visible.

::用于一次性绑定。一旦表达稳定,即在第一次消化后,表达式将停止重新计算。 因此,对某些内容所做的任何更新都不可见。

14-angular controller之间通信方式

发表于 2019-03-27 | 分类于 前端-06-Angular

博客园

在 Angular 中,Controller 之间通信的方式主要有三种:

1)作用域继承。利用子 Controller 控制父 Controller 上的数据。(父 Controller 中的数据要为引用类型,不能是基本类型)

2)注入服务。把需要共享的数据注册为一个 service,在需要的 Controller 中注入。

3)基于事件。利用 Angular 的事件机制,使用 $on、$emit 和 $boardcast

1…456…33
静修

静修

322 日志
19 分类
19 标签
© 2019 静修
本站访客数:
由 Hexo 强力驱动
主题 - NexT.Pisces