
获得徽章 8
function async1(){
return new Promise(function v1(resolve) {
console.log('async1 start');
console.log('await before');
resolve(async2());
}).then(function v11(str) {
console.log(str);
console.log('async1 end');
})
}
function async2() {
return new Promise(function v2(resolve) {
new Promise(function v22(res) {
res(4444);
})
resolve(111);
});
}
function async3(){
return new Promise(function v3(resolve) {
resolve(async1());
}).then(function v33() {
new Promise(function v333(resolve) {
console.log('promise3');
resolve();
}).then(function v3333() {
console.log('promise4');
});
})
}
async3();
new Promise(function newFn(resolve) {
console.log('promise1');
resolve();
}).then(function newThen() {
console.log('promise2');
})
console.log('script end');
为啥promise2会比async1 end先执行??不应该先打印async1 end吗 v11微任务先注册的
return new Promise(function v1(resolve) {
console.log('async1 start');
console.log('await before');
resolve(async2());
}).then(function v11(str) {
console.log(str);
console.log('async1 end');
})
}
function async2() {
return new Promise(function v2(resolve) {
new Promise(function v22(res) {
res(4444);
})
resolve(111);
});
}
function async3(){
return new Promise(function v3(resolve) {
resolve(async1());
}).then(function v33() {
new Promise(function v333(resolve) {
console.log('promise3');
resolve();
}).then(function v3333() {
console.log('promise4');
});
})
}
async3();
new Promise(function newFn(resolve) {
console.log('promise1');
resolve();
}).then(function newThen() {
console.log('promise2');
})
console.log('script end');
为啥promise2会比async1 end先执行??不应该先打印async1 end吗 v11微任务先注册的
展开
2
点赞
请优化:
const str = '[zh-CN]中文[en-EN]English[init-Lang]Init';
const nowLang = 'zh-CN';
const arr = str.match(/\[.+?\]/g)
let newMap = new Map();
const objData = {};
let beforeLang = null;
for(let i = 0; i < arr.length; i++){
const index = str.indexOf(arr[i]);
const langKey = arr[i].slice(1, -1);
const obj = {
start: index,
len: arr[i].length,
};
if(beforeLang){
const startIndex = newMap.get(beforeLang).start;
const len = newMap.get(beforeLang).len;
objData[beforeLang] = str.substring(startIndex + len, index)
}
if(i === arr.length - 1){
objData[langKey] = str.substring(index + arr[i].length);
}
newMap.set(langKey, obj);
beforeLang = langKey;
}
console.log(objData);
/**
* { 'zh-CN': '中文', 'en-EN': 'English', 'init-Lang': 'Init' }
*/
const str = '[zh-CN]中文[en-EN]English[init-Lang]Init';
const nowLang = 'zh-CN';
const arr = str.match(/\[.+?\]/g)
let newMap = new Map();
const objData = {};
let beforeLang = null;
for(let i = 0; i < arr.length; i++){
const index = str.indexOf(arr[i]);
const langKey = arr[i].slice(1, -1);
const obj = {
start: index,
len: arr[i].length,
};
if(beforeLang){
const startIndex = newMap.get(beforeLang).start;
const len = newMap.get(beforeLang).len;
objData[beforeLang] = str.substring(startIndex + len, index)
}
if(i === arr.length - 1){
objData[langKey] = str.substring(index + arr[i].length);
}
newMap.set(langKey, obj);
beforeLang = langKey;
}
console.log(objData);
/**
* { 'zh-CN': '中文', 'en-EN': 'English', 'init-Lang': 'Init' }
*/
展开
4
1