当前,我在存储操作中使用package com.company;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
// write your code here
String myString = "{\n" +
" \"8\": [\n" +
" {\n" +
" \"TEST\": \"LN17ELJ\",\n" +
" \"ROUTE_UNIQUE_ID_REFERENCE\": \"2172752\",\n" +
" \"ORDER_UNIQUE_ID_REFERENCE\": \"109197634\",\n" +
" \"STATUS\": \"HORLEY\",\n" +
" \"SECONDARY_NAV_CITY\": \"HORLEY\",\n" +
" \"ROUTE\": \"THE STREET 12\",\n";
String myRegexPattern = "\"([ROUTE_UNIQUE_ID_REFERENCE\"]+)\"\\s*:\\s*\"([^\"]+)\",?";
String[] newValue = myString.split(myRegexPattern);
for(int i = 0; i < newValue.length; i++) {
if(newValue[i].equals("2172752")) {
System.out.println("IT'S HERE!");
}
}
}
}
,但希望将其转换为promises
。这是带有promise的存储操作的示例:
async/await
此fetchActiveWorkspace (context, workspaceID) {
if (workspaceID) {
return this.$axios.get(`@api-v01/workspaces/workspace/${workspaceID}`)
.then(response => {
context.commit('setActiveWorkspace', response.data)
})
.catch(err => {
throw err
})
} else {
return Promise.resolve(true)
}
},
操作在组件中得以解决,因为它返回了fetchActiveWorkspace
。如何将该代码段转换为promise
结构并在组件中使用?
答案 0 :(得分:0)
这就是我尝试翻译的方式;考虑到由于我无法在完整上下文中访问原始代码,因此我无法直接尝试以确保其有效;但这仍然是您可以在承诺中使用async/await
的方式。
// 1. Mark the function as `async` (otherwise you cannot use `await` inside of it)
async fetchActiveWorkspace(context, workspaceID) {
if (workspaceID) {
// 2. Call the promise-returning function with `await` to wait for result before moving on.
// Capture the response in a varible (it used to go as argument for `then`)
let response = await this.$axios.get(`@api-v01/workspaces/workspace/${workspaceID}`);
context.commit('setActiveWorkspace', response.data);
}
// 3. I don't think this is necessary, as actions are not meant to return values and instead should be for asynchronous mutations.
else {
return true;
}
}
如果要捕获和处理异常,可以用try/catch
包围函数的主体。我之所以没有添加它是为了使事情简单,因为您基于承诺的代码将仅捕获并重新抛出异常,而无需执行其他任何操作。