s*****w 发帖数: 1527 | 1 my dummy code, try to find values from file, then return it.
try {
parse string from file
if find pattern, throw "found it"
} catch (e) {
if(e !== "found it") throw e
} finally {
res.json("pattern1":v1, "pattern2":v2});
}
问题是,假如文件中没找到,应该如何送一个空的json回去?
第二个throw处 (inside catch )应该如何处理?
many thanks ! |
f*******t 发帖数: 7549 | 2 ...
} catch (e) {
if (e == "found it") {
res.json("");
return;
}
throw e;
}
别用finally |
n*****t 发帖数: 22014 | 3 我去 。。。这么喜欢 throw 啊
【在 s*****w 的大作中提到】 : my dummy code, try to find values from file, then return it. : try { : parse string from file : if find pattern, throw "found it" : } catch (e) { : if(e !== "found it") throw e : } finally { : res.json("pattern1":v1, "pattern2":v2}); : } : 问题是,假如文件中没找到,应该如何送一个空的json回去?
|
l**********n 发帖数: 8443 | 4 同学,finally是无论如何都执行的。
【在 s*****w 的大作中提到】 : my dummy code, try to find values from file, then return it. : try { : parse string from file : if find pattern, throw "found it" : } catch (e) { : if(e !== "found it") throw e : } finally { : res.json("pattern1":v1, "pattern2":v2}); : } : 问题是,假如文件中没找到,应该如何送一个空的json回去?
|
s*****w 发帖数: 1527 | 5 我知道我写的是错的,问题是正确方法是什么
【在 l**********n 的大作中提到】 : 同学,finally是无论如何都执行的。
|
f*******t 发帖数: 7549 | 6 我不是给出答案了吗,还问什么?
【在 s*****w 的大作中提到】 : 我知道我写的是错的,问题是正确方法是什么
|
s*****w 发帖数: 1527 | 7 这个有些疑问,
1. finally不是很推崇的吗?
2. throw e 后面怎么处理?不会写
多谢!
【在 f*******t 的大作中提到】 : ... : } catch (e) { : if (e == "found it") { : res.json(""); : return; : } : throw e; : } : 别用finally
|
b***i 发帖数: 3043 | 8 a={""};
try {
parse string from file
if find pattern, throw "found it"
} catch (e) {
if(e !== "found it") throw e
a={"pattern1":v1, "pattern2":v2};
} finally {
res.json(a);
}
【在 s*****w 的大作中提到】 : my dummy code, try to find values from file, then return it. : try { : parse string from file : if find pattern, throw "found it" : } catch (e) { : if(e !== "found it") throw e : } finally { : res.json("pattern1":v1, "pattern2":v2}); : } : 问题是,假如文件中没找到,应该如何送一个空的json回去?
|
l**********n 发帖数: 8443 | 9 new Promise(function(resolve, reject){
let pattern = doParse(file);
resolve(pattern);
}).then(function(pattern){
res.json("pattern1":v1, "pattern2":v2});
})
.catch(function(exp){
res.json(exp);
})
【在 s*****w 的大作中提到】 : my dummy code, try to find values from file, then return it. : try { : parse string from file : if find pattern, throw "found it" : } catch (e) { : if(e !== "found it") throw e : } finally { : res.json("pattern1":v1, "pattern2":v2}); : } : 问题是,假如文件中没找到,应该如何送一个空的json回去?
|
s*****w 发帖数: 1527 | 10 好像没改啥?
【在 b***i 的大作中提到】 : a={""}; : try { : parse string from file : if find pattern, throw "found it" : } catch (e) { : if(e !== "found it") throw e : a={"pattern1":v1, "pattern2":v2}; : } finally { : res.json(a); : }
|
s*****w 发帖数: 1527 | 11 嚓,promise in node.js, 头晕死了
【在 l**********n 的大作中提到】 : new Promise(function(resolve, reject){ : let pattern = doParse(file); : resolve(pattern); : }).then(function(pattern){ : res.json("pattern1":v1, "pattern2":v2}); : }) : .catch(function(exp){ : res.json(exp); : })
|