Note: done is new to PromiseKit 5. We previously defined a variant of then that did not require you to return a promise. Unfortunately, this convention often confused Swift and led to odd and hard-to-debug error messages. It also made using PromiseKit more painful. The introduction of done lets you type out promise chains that compile without additional qualification to help the compiler figure out type information.
firstly { login() }.then { creds in fetch(avatar: creds.user) }.done { image in self.imageView = image }.catch { // any errors in the whole chain land here }
Swift emits a warning if you forget to catch a chain. But we’ll talk about that in more detail later.
每个promise都是一个对象,代表一个单独的异步任务。 如果任务失败, promise就会变成rejected。 Chains that contain rejected 包含rejected的promise链跳过所有后续的promise。相反,执行下一个捕获。 (严格来说,所有后续的捕获处理程序都会被执行。)
Note: PromiseKit has perhaps capriciously switched between the names always and ensure for this function several times in the past. Sorry about this. We suck.
You can also use finally as an ensure that terminates the promise chain and does not return a value: 您也可以使用finally作为结束的promise链且不返回值的ensure:
firstly { CLLocationManager.promise() }.then { location in CLGeocoder.reverseGeocode(location) }.done { placemarks in self.placemark.text = "\(placemarks.first)" }
要使用这些扩展,您需要指定子代码:
1 2 3
pod "PromiseKit" pod "PromiseKit/CoreLocation" pod "PromiseKit/MapKit"
All of these extensions are available at the PromiseKit organization. Go there to see what’s available and to read the source code and documentation. Every file and function has been copiously documented.
We also provide extensions for common libraries such as Alamofire.
Making Promises
The standard extensions will take you a long way, but sometimes you’ll still need to start chains of your own. Maybe you’re using a third party API that doesn’t provide promises, or perhaps you wrote your own asynchronous system. Either way, it’s easy to add promises. If you look at the code of the standard extensions, you’ll see that it uses the same approach described below.
funcfetch() -> Promise<String> { returnPromise { seal in fetch { result, error in seal.resolve(result, error) } } }
The seal object that the Promise initializer provides to you defines many methods for handling garden-variety completion handlers. It even covers a variety of rarer situations, thus making it easy for you to add promises to an existing codebase.
Note: We tried to make it so that you could just do Promise(fetch), but we were not able to make this simpler pattern work universally without requiring extra disambiguation for the Swift compiler. Sorry; we tried.
Note: In PMK 4, this initializer provided two parameters to your closure: fulfill and reject. PMK 5 and 6 give you an object that has both fulfill and reject methods, but also many variants of the method resolve. You can typically just pass completion handler parameters to resolve and let Swift figure out which variant to apply to your particular case (as shown in the example above).
NoteGuarantees (below) have a slightly different initializer (since they cannot error) so the parameter to the initializer closure is just a closure. Not a Resolver object. Thus do seal(value) rather than seal.fulfill(value). This is because there is no variations in what guarantees can be sealed with, they can only fulfill.
Guarantee<T>
Since PromiseKit 5, we have provided Guarantee as a supplementary class to Promise. We do this to complement Swift’s strong error handling system.
Guarantees never fail, so they cannot be rejected. A good example is after:
1 2 3 4 5
firstly { after(seconds: 0.1) }.done { // there is no way to add a `catch` because after cannot fail. }
Swift warns you if you don’t terminate a regular Promise chain (i.e., not a Guarantee chain). You’re expected to silence this warning by supplying either a catch or a return. (In the latter case, you will then have to catch at the point where you receive that promise.)
Use Guarantees wherever possible so that your code has error handling where it’s required and no error handling where it’s not required.
In general, you should be able to use Guarantees and Promises interchangeably, We have gone to great lengths to try and ensure this, so please open a ticket if you find an issue.
If you are creating your own guarantees the syntax is simpler than that of promises;
1 2 3 4 5 6 7
funcfetch() -> Promise<String> { returnGuarantee { seal in fetch { result in seal(result) } } }
then provides you with the result of the previous promise and requires you to return another promise.
map provides you with the result of the previous promise and requires you to return an object or value type.
compactMap provides you with the result of the previous promise and requires you to return an Optional. If you return nil, the chain fails with PMKError.compactMap.
Rationale: Before PromiseKit 4, then handled all these cases, and it was painful. We hoped the pain would disappear with new Swift versions. However, it has become clear that the various pain points are here to stay. In fact, we as library authors are expected to disambiguate at the naming level of our API. Therefore, we have split the three main kinds of then into then, map and done. After using these new functions, we realized this is much nicer in practice, so we added compactMap as well (modeled on Optional.compactMap).
compactMap facilitates quick composition of promise chains. For example:
1 2 3 4 5 6 7 8 9 10
firstly { URLSession.shared.dataTask(.promise, with: rq) }.compactMap { tryJSONSerialization.jsonObject($0.data) as? [String] }.done { arrayOfStrings in //… }.catch { error in // Foundation.JSONError if JSON was badly formed // PMKError.compactMap if JSON was of different type }
Tip: We also provide most of the functional methods you would expect for sequences, e.g., map, thenMap, compactMapValues, firstValue, etc.
get
We provide get as a done that returns the value fed to get.
1 2 3 4 5 6 7
firstly { foo() }.get { foo in //… }.done { foo in // same foo! }
tap
We provide tap for debugging. It’s the same as get but provides the Result<T> of the Promise so you can inspect the value of the chain at this point without causing any side effects:
npm WARN deprecated joi@14.3.1: This module has moved and is now available at @hapi/joi. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues. npm WARN deprecated topo@3.0.3: This module has moved and is now available at @hapi/topo. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues. npm WARN deprecated hoek@6.1.3: This module has moved and is now available at @hapi/hoek. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues. npm WARN deprecated cross-spawn-async@2.2.5: cross-spawn no longer requires a build toolchain, use it instead npm ERR! Unexpected end of JSON input while parsing near '...TGOVzYcDOP1jLScCp0ACN'
npm ERR! A complete log of this run can be found in: npm ERR! /Users/sam/.npm/_logs/2019-04-29T01_23_19_163Z-debug.log
清楚一下缓存npm cache clean --force
运行vue --version出现版本信息则说明安装成功
创建新项目vue create my-app
使用默认安装
1 2 3 4
Vue CLI v3.7.0 ? Please pick a preset: (Use arrow keys) ❯ default (babel, eslint) Manually select features
◉ Babel ◯ TypeScript ◯ Progressive Web App (PWA) Support ◉ Router ◉ Vuex ◉ CSS Pre-processors ◉ Linter / Formatter ❯◉ Unit Testing ◯ E2E Testing
其他选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14
? Use history mode for router? (Requires proper server setup for index fallback in produc tion) Yes ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by defaul t): Less ? Pick a linter / formatter config: Prettier ? Pick additional lint features: ◉ Lint on save ❯◉ Lint and fix on commit ? Pick additional lint features: Lint on save, Lint and fix on commit ? Pick a unit testing solution: Jest ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files ? Save this as a preset for future projects? Yes ? Save preset as: vue-app ? Pick the package manager to use when installing dependencies: Yarn
// https://github.com/ant-design/ant-motion/issues/44 .bezierEasingMixin(); ^ Inline JavaScript is not enabled. Is it setin your options? in /Users/samtake/Documents/GitHub/Vue_app/vue-app/node_modules/ant-design-vue/lib/style/color/bezierEa sing.less (line 110, column 0)
int boxVolume(int length, int width, int height){ return length*width*height; }
一元作用域运算符
C++提供了一元作用域运算符,可以在含有与全局变量名的局部变量的域中访问该全局变量。
1 2 3 4 5 6 7 8 9
int number = 7; void testUnaryOperation(){ double number = 10.7; cout << "location number = " << number <<endl; cout << "gloable number = " << ::number <<endl; }
void VertifyWithSetFunc::setCourseName(string name){ if (name.length() <= 10) { courseName = name ; } if (name.length() > 10) { courseName = name.substr(0, 10); cout<< "name is long than 10 :"<< name << endl; } }
输出
1 2 3
name is long than 10 :test2test2test2test2test2test2test2test2test2test2 vertify1: test1 vertify2: test2test2
报错、报错、报错、报错、 nasm/yasm not found or too old. Use --disable-x86asm for a crippled build. ERROR: libfdk_aac not found ERROR: speex not found using pkg-config ERROR: libx264 not found
==> Caveats gettext is keg-only, which means it was not symlinked into /usr/local, because macOS provides the BSD gettext library & some software gets confused if both are in the library path.
If you need to have gettext first in your PATH run: echo'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.zshrc
For compilers to find gettext you may need to set: export LDFLAGS="-L/usr/local/opt/gettext/lib" export CPPFLAGS="-I/usr/local/opt/gettext/include" ==> Caveats libffi is keg-only, which means it was not symlinked into /usr/local, because some formulae require a newer version of libffi.
For compilers to find libffi you may need to set: export LDFLAGS="-L/usr/local/opt/libffi/lib"
For pkg-config to find libffi you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
==> Caveats ==> gettext gettext is keg-only, which means it was not symlinked into /usr/local, because macOS provides the BSD gettext library & some software gets confused if both are in the library path.
If you need to have gettext first in your PATH run: echo'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.zshrc
For compilers to find gettext you may need to set: export LDFLAGS="-L/usr/local/opt/gettext/lib" export CPPFLAGS="-I/usr/local/opt/gettext/include"
==> libffi libffi is keg-only, which means it was not symlinked into /usr/local, because some formulae require a newer version of libffi.
For compilers to find libffi you may need to set: export LDFLAGS="-L/usr/local/opt/libffi/lib"
For pkg-config to find libffi you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
==> unbound To have launchd start unbound now and restart at startup: sudo brew services start unbound ==> glib Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> tesseract This formula contains only the "eng", "osd", and "snum" language data files. If you need any other supported languages, run `brew install tesseract-lang`.
brew info ffmpeg可以看到x264, x265这些包都是有安装的。升级则通过brew update && brew upgrade ffmpeg.
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.